變量可以臨時(shí)保存數(shù)據(jù),因此可以把數(shù)據(jù)保存在變量中,以便進(jìn)一步操作。
#定義變量$a=10$b=4#計(jì)算變量$result=$a*$b$msg="保存文本"#輸出變量$result$msg40保存文本
powershell 不需要顯示地去聲明,可以自動(dòng)創(chuàng)建變量,只須記住變量的前綴為$.
創(chuàng)建好了變量后,可以通過(guò)變量名輸出變量,也可以把變量名存在字符串中。但是有個(gè)例外單引號(hào)中的字符串不會(huì)識(shí)別和處理變量名。
選擇變量名
在powershell中變量名均是以美元符”$”開(kāi)始,剩余字符可以是數(shù)字、字母、下劃線的任意字符,并且powershell變量名大小寫不敏感($a和$A 是同一個(gè)變量)。
某些特殊的字符在powershell中有特殊的用途,一般不推薦使用這些字符作為變量名。當(dāng)然你硬要使用,請(qǐng)把整個(gè)變量名后綴用花括號(hào)括起來(lái)。
PS C:/test> ${"I"like $}="mossfly"PS C:/test> ${"I"like $}
mossfly賦值和返回值
賦值操作符為“=”,幾乎可以把任何數(shù)據(jù)賦值給一個(gè)變量,甚至一條cmdlet命令
,為什么,因?yàn)镻owershell支持對(duì)象,對(duì)象可以包羅萬(wàn)象。
PS C:/test> $item=Get-ChildItem .PS C:/test> $item Directory: C:/testMode LastWriteTime Length Name---- ------------- ------ ----d---- 2011/11/23 17:25 ABC-a--- 2011/11/24 18:30 67580 a.html-a--- 2011/11/24 20:04 26384 a.txt-a--- 2011/11/24 20:26 12060 alias-a--- 2011/11/24 20:27 12060 alias.ps1-a--- 2011/11/23 17:25 0 b.txt-a--- 2011/11/23 17:25 0 c.txt-a--- 2011/11/23 17:25 0 d.txt-a--- 2011/11/25 11:20 556 employee.xml-a--- 2011/11/24 17:37 7420 name.html-a--- 2011/11/28 15:30 63 ping.bat-a--- 2011/11/24 17:44 735892 Powershell_Cmdlets.html-a--- 2011/11/28 17:03 60 test.ps1-a--- 2011/11/23 17:37 242 test.txt-a--- 2011/11/28 16:42 170 test.vbsPS C:/test> $result=3000*(1/12+0.0075)PS C:/test> $result272.5
給多個(gè)變量同時(shí)賦值
賦值操作符不僅能給一個(gè)變量賦值,還可以同時(shí)給多個(gè)變量賦相同的值。
PS C:/test> $a=$b=$c=123PS C:/test> $a123PS C:/test> $b123PS C:/test> $c123
交換變量的值
要交換兩個(gè)變量的值,傳統(tǒng)的程序語(yǔ)言至少需要三步,并且還需定義一個(gè)中間臨時(shí)變量。
$Value1 = 10$Value2 = 20$Temp = $Value1$Value1 = $Value2$Value2 = $Temp
在powershell中,交換兩個(gè)變量的值,這個(gè)功能變得非常簡(jiǎn)單。
PS C:/test> $value1=10PS C:/test> $value2=20PS C:/test> $value1,$value2=$value2,$value1PS C:/test> $value120PS C:/test> $value210
查看正在使用的變量
Powershell將變量的相關(guān)信息的記錄存放在名為variable:的驅(qū)動(dòng)中。如果要查看所有定義的變量,可以直接遍歷variable:
PS C:/test> ls variable:Name Value---- -----"I"like $ mossfly$ cls? True^ cls_1 1a 123args {}b 123c 123ConfirmPreference HighConsoleFileNameDebugPreference SilentlyContinue。。。
查找變量
因?yàn)橛刑摂M驅(qū)動(dòng)variable:的存在,可以象查找文件那樣使用通配符查找變量。例如要查詢以value打頭的變量名。
PS C:/test> ls variable:value*Name Value---- -----value1 20value2 10
驗(yàn)證變量是否存在
驗(yàn)證一個(gè)變量是否存在,仍然可以象驗(yàn)證文件系統(tǒng)那樣,使用cmdlet Test-Path。為什么?因?yàn)樽兞看嬖谧兞框?qū)動(dòng)器中。
PS C:/test> Test-Path variable:value1TruePS C:/test> Test-Path variable:value2TruePS C:/test> Test-Path variable:valueUnkonwFalse
刪除變量
因?yàn)樽兞繒?huì)在powershell退出或關(guān)閉時(shí),自動(dòng)清除。一般沒(méi)必要?jiǎng)h除,但是你非得刪除,也可以象刪除文件那樣刪除它。
PS C:/test> Test-Path variable:value1TruePS C:/test> del variable:value1PS C:/test> Test-Path variable:value1False
使用專用的變量命令
為了管理變量,powershell提供了五個(gè)專門管理變量的命令Clear-Variable,Get-Variable,New-Variable,Remove-Variable,Set-Variable。因?yàn)樘摂M驅(qū)動(dòng)器variable:的存在,clear,remove,set打頭的命令可以被代替。但是Get-Variable,New-Variable。卻非常有用new-variable可以在定義變量時(shí),指定變量的一些其它屬性,比如訪問(wèn)權(quán)限。同樣Get-Variable也可以獲取這些附加信息。
變量寫保護(hù)
可以使用New-Variable 的option選項(xiàng) 在創(chuàng)建變量時(shí),給變量加上只讀屬性,這樣就不能給變量重新賦值了。
PS C:/test> New-Variable num -Value 100 -Force -Option readonlyPS C:/test> $num=101Cannot overwrite variable num because it is read-only or constant.At line:1 char:5+ $num <<<< =101 + CategoryInfo : WriteError: (num:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritable PS C:/test> del Variable:numRemove-Item : Cannot remove variable num because it is constant or read-only. If the variable is read-only,ration again specifying the Force option.At line:1 char:4+ del <<<< Variable:num + CategoryInfo : WriteError: (num:String) [Remove-Item], SessionStateUnauthorizedAccessExcepti + FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveItemCommand
但是可以通過(guò)刪除變量,再重新創(chuàng)建變量更新變量?jī)?nèi)容。
PS C:/test> del Variable:num -ForcePS C:/test> $num=101PS C:/test> $num101
有沒(méi)有權(quán)限更高的變量,有,那就是:選項(xiàng)Constant,常量一旦聲明,不可修改
PS C:/test> new-variable num -Value "strong" -Option constantPS C:/test> $num="why? can not delete it."Cannot overwrite variable num because it is read-only or constant.At line:1 char:5+ $num <<<< ="why? can not delete it." + CategoryInfo : WriteError: (num:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritable PS C:/test> del Variable:num -ForceRemove-Item : Cannot remove variable num because it is constant or read-only. If the variable is read-only,ration again specifying the Force option.At line:1 char:4+ del <<<< Variable:num -Force + CategoryInfo : WriteError: (num:String) [Remove-Item], SessionStateUnauthorizedAccessExcepti + FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveItemCommand
變量描述
在New-Variable 可以通過(guò)-description 添加變量描述,但是變量描述默認(rèn)不會(huì)顯示,可以通過(guò)Format-List 查看。
PS C:/test> new-variable name -Value "me" -Description "This is my name"PS C:/test> ls Variable:name | fl *PSPath : Microsoft.PowerShell.CoreVariable::namePSDrive : VariablePSProvider : Microsoft.PowerShell.CoreVariablePSIsContainer : FalseName : nameDescription : This is my nameValue : meVisibility : PublicModule :ModuleName :Options : NoneAttributes : {}
新聞熱點(diǎn)
疑難解答
圖片精選