Windows Powershell 变量的幕后管理

2019-09-30 15:13:10王冬梅

PS> $var=123
PS> (Get-Variable var).Attributes
PS> $var.GetType().FullName
System.Int32
PS> $var="字符串"
PS> (Get-Variable var).Attributes
PS> $var.GetType().FullName
System.String
PS> [int]$var=123
PS> (Get-Variable var).Attributes

TypeId
------
System.Management.Automation.ArgumentTypeConverterAttribute

PS> $var.GetType().FullName
System.Int32
PS> $var="2012"
PS> $var
2012
PS> $var.GetType().FullName
System.Int32
PS> $var="2012世界末日"
Cannot convert value "2012世界末日" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:5
+ $var <<<< ="2012世界末日"   + CategoryInfo     : MetadataError: (:) [], ArgumentTransformationMetadataException   + FullyQualifiedErrorId : RuntimeException PS> (Get-Variable var).Attributes.Clear()
PS> (Get-Variable var).Attributes
PS> $var="2012世界末日"
PS> $var.GetType().FullName
System.String

验证和检查变量的内容
变量PSVariable对象的Attributes属性能够存储一些附件条件,例如限制变量的长度,这样在变量重新赋值时就会进行验证,下面演示如何限制一个字符串变量的长度为位于2-5之间。

PS> $var="限制变量"
PS> $condition= New-Object System.Management.Automation.ValidateLengthAttribute -ArgumentList 2,5
PS> (Get-Variable var).Attributes.Add($condition)
PS> $var="限制"
PS> $var="射雕英雄传"
PS> $var="看射雕英雄传"
The variable cannot be validated because the value 看射雕英雄传 is not a valid value for the var variable.
At line:1 char:5
+ $var <<<< ="看射雕英雄传"
  + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException
  + FullyQualifiedErrorId : ValidateSetFailure

常用的变量内容验证还有5种,分别为:
ValidateNotNullAttribute:限制变量不能为空
ValidateNotNullOrEmptyAttribute:限制变量不等为空,不能为空字符串,不能为空集合
ValidatePatternAttribute:限制变量要满足制定的正则表达式
ValidateRangeAttribute:限制变量的取值范围
ValidateSetAttribute:限制变量的取值集合

ValidateNotNullAttribute 例子

PS> $a=123
PS> $con=New-Object System.Management.Automation.ValidateNotNullAttribute
PS> (Get-Variable a).Attributes.Add($con)
PS> $a=8964
PS> $a=$null


无法验证此变量,因为值  不是变量 a 的有效值。
所在位置 行:1 字符: 3

+ $a <<<< =$null
  + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException
  + FullyQualifiedErrorId : ValidateSetFailure
ValidateNotNullOrEmptyAttribute 

例子,注意@()为一个空数组。

PS> $con=New-Object System.Management.Automation.ValidateNotNullOrEmptyAttribute
PS> (Get-Variable a).Attributes.clear()
PS> (Get-Variable a).Attributes.add($con)
PS> $a=$null
The variable cannot be validated because the value is not a valid value for the a variable.
At line:1 char:3
+ $a <<<< =$null   + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException   + FullyQualifiedErrorId : ValidateSetFailure PS> $a=""
The variable cannot be validated because the value is not a valid value for the a variable.
At line:1 char:3
+ $a <<<< =""   + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException   + FullyQualifiedErrorId : ValidateSetFailure PS> $a=@()
The variable cannot be validated because the value System.Object[] is not a valid value for the a variable.
At line:1 char:3
+ $a <<<< =@()
  + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException
  + FullyQualifiedErrorId : ValidateSetFailureValidatePatternAttribute 例子,验证Email格式

PS> $email="test@mossfly.com"
PS> $con=New-Object System.Management.Automation.ValidatePatternAttribute "b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}b"
PS> (Get-Variable email).Attributes.Add($con)
PS> $email="abc@abc.com"
PS> $email="abc@mossfly.com"
PS> $email="author@gmail.com"
PS> $email="www@mossfly"
The variable cannot be validated because the value www@mossfly is not a valid value for the email variable.
At line:1 char:7
+ $email <<<< ="www@mossfly"
  + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException
  + FullyQualifiedErrorId : ValidateSetFailureValidateRangeAttribute 例子,验证月份1-12

PS> $month=1
PS> (Get-Variable month).Attributes.Add( $( New-Object System.Management.Automation.ValidateRangeAttribute -ArgumentList 1,12) )
PS> $month=10
PS> $month=12
PS> $month=18
The variable cannot be validated because the value 18 is not a valid value for the month variable.
At line:1 char:7
+ $month <<<< =18   + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException   + FullyQualifiedErrorId : ValidateSetFailure ValidateSetAttribute 例子,验证性别 PS> $sex="男"
PS> $con=New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList "男","女","保密"
PS> (Get-Variable sex).Attributes.Add($con)
PS> $sex="女"
PS> $sex="不男不女"
The variable cannot be validated because the value 不男不女 is not a valid value for the sex variable.
At line:1 char:5
+ $sex <<<< ="不男不女"
  + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException
  + FullyQualifiedErrorId : ValidateSetFailure