Windows Powershell 變量的幕后管理
在Powershell中創(chuàng)建一個(gè)變量,會(huì)在后臺(tái)生成一個(gè)PSVariable對(duì)象,這個(gè)對(duì)象不僅包含變量的值,也包含變量的其它信息,例如”只寫保護(hù)”這樣的描述。
如果在Powershell中輸出一個(gè)變量,只會(huì)輸出這個(gè)變量的值。不能夠顯示它的其它信息,如果想查看一個(gè)變量的其它保留信息,就需要變量的基類PSVariable對(duì)象,這個(gè)可以通過Get-Variable命令得到,下面的例子演示如何查看一個(gè)變量的全部信息。
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 : {}
修改變量的選項(xiàng)設(shè)置
Powershell處理一個(gè)變量的PSVariable對(duì)象,主要是為了能夠更新變量的選項(xiàng)設(shè)置。既可以使用命令Set-Variable,也可以在獲取PSvariable對(duì)象后直接更改。比如更改一個(gè)變量的描述:
PS> $str="我是一個(gè)變量"
PS> $var=Get-Variable str
PS> $var
Name Value
---- -----
str 我是一個(gè)變量
PS> $var | fl *
Name : str
Description :
Value : 我是一個(gè)變量
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}
PS> $var.Description="我知道你是一個(gè)變量"
PS> $var | fl *
Name : str
Description : 我知道你是一個(gè)變量
Value : 我是一個(gè)變量
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}如果你不想多加一個(gè)臨時(shí)變量$var來存儲(chǔ)PSVariable,可以使用Powershell子表達(dá)式
PS> (Get-Variable str).Description="變量的描述已更改;"
PS> Get-Variable str | Format-Table Name,Description
Name Description
---- -----------
str 變量的描述已更改;
激活變量的寫保護(hù)
可以操作一個(gè)變量的選項(xiàng)設(shè)置 ,比如給一個(gè)變量加上寫保護(hù),需要將Option設(shè)置為“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
變量的選項(xiàng)
變量的選項(xiàng)是一個(gè)枚舉值,包含:
“None”:默認(rèn)設(shè)置
“ReadOnly”:變量只讀,但是可以通過-Force 選項(xiàng)更新。
“Constant”:常量一旦聲明,在當(dāng)前控制臺(tái)不能更新。
“Private”:只在當(dāng)前作用域可見,不能貫穿到其它作用域
“AllScope”:全局,可以貫穿于任何作用域
變量的類型規(guī)范
每個(gè)變量的都有自己的類型,這個(gè)具體的類型存放在PsVariable對(duì)象的Attributes[System.Management.Automation.PSVariableAttributeCollection]屬性,如果這個(gè)Attributes為空,可以給這個(gè)變量存放任何 類型的數(shù)據(jù),Powershell會(huì)自己選擇合適的類型。一旦這個(gè)Attributes屬性確定下來,就不能隨意存放數(shù)據(jù)了。例如給$var存放一個(gè)整數(shù),屬于弱類型,所以Attributes屬性為空,這時(shí)還可以給它賦值一個(gè)字符串。但是如果給$var增加強(qiáng)類型,存放一個(gè)整數(shù),再給它賦值一個(gè)其它類型,解釋器會(huì)自動(dòng)嘗試轉(zhuǎn)換,如果不能轉(zhuǎn)換就會(huì)拋出異常。這時(shí)如果你非得更新$var變量的類型,可以使用 (Get-Variable var).Attributes.Clear(),清空Attributes,這樣強(qiáng)類型就又轉(zhuǎn)換成弱類型了。
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
驗(yàn)證和檢查變量的內(nèi)容
變量PSVariable對(duì)象的Attributes屬性能夠存儲(chǔ)一些附件條件,例如限制變量的長度,這樣在變量重新賦值時(shí)就會(huì)進(jìn)行驗(yàn)證,下面演示如何限制一個(gè)字符串變量的長度為位于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
常用的變量內(nèi)容驗(yàn)證還有5種,分別為:
ValidateNotNullAttribute:限制變量不能為空
ValidateNotNullOrEmptyAttribute:限制變量不等為空,不能為空字符串,不能為空集合
ValidatePatternAttribute:限制變量要滿足制定的正則表達(dá)式
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
無法驗(yàn)證此變量,因?yàn)橹?nbsp; 不是變量 a 的有效值。
所在位置 行:1 字符: 3
+ $a <<<< =$null + CategoryInfo : MetadataError: (:) [], ValidationMetadataException + FullyQualifiedErrorId : ValidateSetFailure ValidateNotNullOrEmptyAttribute
例子,注意@()為一個(gè)空數(shù)組。
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 例子,驗(yàn)證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 例子,驗(yàn)證月份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 例子,驗(yàn)證性別 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
相關(guān)文章
PowerShell使用正則表達(dá)式查找字符串實(shí)例
這篇文章主要介紹了PowerShell使用正則表達(dá)式查找字符串實(shí)例,主要是對(duì)match運(yùn)算符的使用介紹,需要的朋友可以參考下2014-08-08
PowerShell中使用Get-Alias命令獲取cmdlet別名例子
這篇文章主要介紹了PowerShell中使用Get-Alias命令獲取cmdlet別名的例子,PowerShell中兼容DOS下和Linux下的很多命令,這些都是靠別名來實(shí)現(xiàn)的,需要的朋友可以參考下2014-08-08
PowerShell默認(rèn)參數(shù)$PSDefaultParameterValues結(jié)合Out-File輸出到日志文件
這篇文章主要介紹了PowerShell默認(rèn)參數(shù)$PSDefaultParameterValues結(jié)合Out-File輸出到日志文件實(shí)例,需要的朋友可以參考下2014-06-06
powershell玩轉(zhuǎn)SQL SERVER所有版本的方法
微軟發(fā)布了最新的powershell for sql server 2016命令行客戶端庫。文章介紹了與之相關(guān)的實(shí)用方法,需要的朋友可以參考下2017-10-10
PowerShell ISE自動(dòng)化簡(jiǎn)單示例
這篇文章主要介紹了PowerShell ISE自動(dòng)化簡(jiǎn)單示例,本文給出了2個(gè)PowerShell ISE自動(dòng)化例子,需要的朋友可以參考下2015-03-03
PowerShell包含另一個(gè)腳本文件和獲取當(dāng)前腳本所在目錄的方法例子
這篇文章主要介紹了PowerShell包含另一個(gè)腳本文件和獲取當(dāng)前腳本所在目錄的方法例子,需要的朋友可以參考下2014-08-08
PowerShell實(shí)現(xiàn)時(shí)間管理小秘書
這篇文章主要介紹了PowerShell實(shí)現(xiàn)時(shí)間管理小秘書,本文是一個(gè)PowerShell的綜合編程實(shí)例,實(shí)現(xiàn)了一個(gè)用來管理時(shí)間的功能,需要的朋友可以參考下2015-04-04
Powershell使用OpenFileDialog打開文件示例
這篇文章主要介紹了Powershell使用OpenFileDialog打開文件示例,本文直接給出示例代碼,需要的朋友可以參考下2015-03-03

