当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell 变量的幕后管理

Windows Powershell 变量的幕后管理

2017年12月08日  | 移动技术网IT编程  | 我要评论

在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 : 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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网