首页 > 开发 > PowerShell > 正文

Windows Powershell 变量的幕后管理

2020-03-26 19:18:15
字体:
来源:转载
供稿:网友

在Powershell中创建一个变量,会在后台生成一个PSVariable对象,这个对象不仅包含变量的值,也包含变量的其它信息,例如”只写保护”这样的描述。
如果在Powershell中输出一个变量,只会输出这个变量的值。不能够显示它的其它信息,如果想查看一个变量的其它保留信息,就需要变量的基类PSVariable对象,这个可以通过Get-Variable命令得到,下面的例子演示如何查看一个变量的全部信息。

PS> $a=get-datePS> Get-Variable aName              Value----              -----a               2011/12/8 17:52:02PS> Get-Variable a | fl *Name    : aDescription :Value    : 2011/12/8 17:52:02Visibility : PublicModule   :ModuleName :Options   : NoneAttributes : {}

修改变量的选项设置
Powershell处理一个变量的PSVariable对象,主要是为了能够更新变量的选项设置。既可以使用命令Set-Variable,也可以在获取PSvariable对象后直接更改。比如更改一个变量的描述:

PS> $str="我是一个变量"PS> $var=Get-Variable strPS> $varName              Value----              -----str              我是一个变量PS> $var | fl *Name    : strDescription :Value    : 我是一个变量Visibility : PublicModule   :ModuleName :Options   : NoneAttributes : {}PS> $var.Description="我知道你是一个变量"PS> $var | fl *Name    : strDescription : 我知道你是一个变量Value    : 我是一个变量Visibility : PublicModule   :ModuleName :Options   : NoneAttributes : {}如果你不想多加一个临时变量$var来存储PSVariable,可以使用Powershell子表达式PS> (Get-Variable str).Description="变量的描述已更改;"PS> Get-Variable str | Format-Table Name,DescriptionName                            Description----                            -----------str                             变量的描述已更改;

激活变量的写保护
可以操作一个变量的选项设置 ,比如给一个变量加上写保护,需要将Option设置为“ReadOnly”

PS> $var="mossfly"PS> Set-Variable var -Option "ReadOnly"PS> (Get-Variable var).OptionsReadOnlyPS> Set-Variable var -Option "None" -ForcePS> (Get-Variable var).OptionsNone

变量的选项
变量的选项是一个枚举值,包含:
“None”:默认设置
“ReadOnly”:变量只读,但是可以通过-Force 选项更新。
“Constant”:常量一旦声明,在当前控制台不能更新。
“Private”:只在当前作用域可见,不能贯穿到其它作用域
“AllScope”:全局,可以贯穿于任何作用域

变量的类型规范
每个变量的都有自己的类型,这个具体的类型存放在PsVariable对象的Attributes[System.Management.Automation.PSVariableAttributeCollection]属性,如果这个Attributes为空,可以给这个变量存放任何 类型的数据,Powershell会自己选择合适的类型。一旦这个Attributes属性确定下来,就不能随意存放数据了。例如给$var存放一个整数,属于弱类型,所以Attributes属性为空,这时还可以给它赋值一个字符串。但是如果给$var增加强类型,存放一个整数,再给它赋值一个其它类型,解释器会自动尝试转换,如果不能转换就会抛出异常。这时如果你非得更新$var变量的类型,可以使用 (Get-Variable var).Attributes.Clear(),清空Attributes,这样强类型就又转换成弱类型了。

PS> $var=123PS> (Get-Variable var).AttributesPS> $var.GetType().FullNameSystem.Int32PS> $var="字符串"PS> (Get-Variable var).AttributesPS> $var.GetType().FullNameSystem.StringPS> [int]$var=123PS> (Get-Variable var).AttributesTypeId------System.Management.Automation.ArgumentTypeConverterAttributePS> $var.GetType().FullNameSystem.Int32PS> $var="2012"PS> $var2012PS> $var.GetType().FullNameSystem.Int32PS> $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).AttributesPS> $var="2012世界末日"PS> $var.GetType().FullNameSystem.String

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

PS> $var="限制变量"PS> $condition= New-Object System.Management.Automation.ValidateLengthAttribute -ArgumentList 2,5PS> (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=123PS> $con=New-Object System.Management.Automation.ValidateNotNullAttributePS> (Get-Variable a).Attributes.Add($con)PS> $a=8964PS> $a=$null


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

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

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

PS> $con=New-Object System.Management.Automation.ValidateNotNullOrEmptyAttributePS> (Get-Variable a).Attributes.clear()PS> (Get-Variable a).Attributes.add($con)PS> $a=$nullThe 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-12PS> $month=1PS> (Get-Variable month).Attributes.Add( $( New-Object System.Management.Automation.ValidateRangeAttribute -ArgumentList 1,12) )PS> $month=10PS> $month=12PS> $month=18The 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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表