Powershell变量的幕后管理 6


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

PS> $a=get-date
PS> Get-Variable a

Name                           Value
----                           -----
a                              2011/12/8 17:52:02

PS> Get-Variable a | fl *

Name        : a
Description :
Value       : 2011/12/8 17:52:02
Visibility  : Public
Module      :
ModuleName  :
Options     : None
Attributes  : {}

修改变量的选项设置

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

PS> $str="我是一个变量"
PS> $var=Get-Variable str
PS> $var

Name                           Value
----                           -----
str                            我是一个变量

PS> $var | fl *

Name        : str
Description :
Value       : 我是一个变量
Visibility  : Public
Module      :
ModuleName  :
Options     : None
Attributes  : {}

PS> $var.Description="我知道你是一个变量"
PS> $var | fl *
Name        : str
Description : 我知道你是一个变量
Value       : 我是一个变量
Visibility  : Public
Module      :
ModuleName  :
Options     : None
Attributes  : {}

如果你不想多加一个临时变量$var来存储PSVariable,可以使用Powershell子表达式

PS> (Get-Variable str).Description="变量的描述已更改;"
PS> Get-Variable str | Format-Table Name,Description

Name                                                        Description
----                                                        -----------
str                                                         变量的描述已更改;

激活变量的写保护

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

PS> $var="mossfly"
PS> Set-Variable var -Option "ReadOnly"
PS> (Get-Variable var).Options
ReadOnly
PS> Set-Variable var -Option "None" -Force
PS> (Get-Variable var).Options
None

变量的选项

变量的选项是一个枚举值,包含:
“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=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 : ValidateSetFailure

ValidatePatternAttribute 例子,验证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 : ValidateSetFailure

ValidateRangeAttribute 例子,验证月份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
本文链接: https://www.pstips.net/powershell-variable-management-behind-the-scenes.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 Hector 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

6 条评论 “Powershell变量的幕后管理