變量可以臨時(shí)保存數(shù)據(jù),因此可以把數(shù)據(jù)保存在變量中,以便進(jìn)一步操作。
#定義變量
$a=10
$b=4
#計(jì)算變量
$result=$a*$b
$msg="保存文本"
#輸出變量
$result
$msg
40
保存文本
powershell 不需要顯示地去聲明,可以自動(dòng)創(chuàng)建變量,只須記住變量的前綴為$.
創(chuàng)建好了變量后,可以通過(guò)變量名輸出變量,也可以把變量名存在字符串中。但是有個(gè)例外單引號(hào)中的字符串不會(huì)識(shí)別和處理變量名。
選擇變量名
在powershell中變量名均是以美元符”$”開(kāi)始,剩余字符可以是數(shù)字、字母、下劃線的任意字符,并且powershell變量名大小寫(xiě)不敏感($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:\test
Mode 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.vbs
PS C:\test> $result=3000*(1/12+0.0075)
PS C:\test> $result
272.5
給多個(gè)變量同時(shí)賦值
賦值操作符不僅能給一個(gè)變量賦值,還可以同時(shí)給多個(gè)變量賦相同的值。
PS C:\test> $a=$b=$c=123
PS C:\test> $a
123
PS C:\test> $b
123
PS C:\test> $c
123
交換變量的值
要交換兩個(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=10
PS C:\test> $value2=20
PS C:\test> $value1,$value2=$value2,$value1
PS C:\test> $value1
20
PS C:\test> $value2
10
查看正在使用的變量
Powershell將變量的相關(guān)信息的記錄存放在名為variable:的驅(qū)動(dòng)中。如果要查看所有定義的變量,可以直接遍歷variable:
PS C:\test> ls variable:
Name Value
---- -----
"I"like $ mossfly
$ cls
? True
^ cls
_
1 1
a 123
args {}
b 123
c 123
ConfirmPreference High
ConsoleFileName
DebugPreference SilentlyContinue
。。。
查找變量
因?yàn)橛刑摂M驅(qū)動(dòng)variable:的存在,可以象查找文件那樣使用通配符查找變量。例如要查詢以value打頭的變量名。
PS C:\test> ls variable:value*
Name Value
---- -----
value1 20
value2 10
驗(yàn)證變量是否存在
驗(yàn)證一個(gè)變量是否存在,仍然可以象驗(yàn)證文件系統(tǒng)那樣,使用cmdlet Test-Path。為什么?因?yàn)樽兞看嬖谧兞框?qū)動(dòng)器中。
PS C:\test> Test-Path variable:value1
True
PS C:\test> Test-Path variable:value2
True
PS C:\test> Test-Path variable:valueUnkonw
False
刪除變量
因?yàn)樽兞繒?huì)在powershell退出或關(guān)閉時(shí),自動(dòng)清除。一般沒(méi)必要?jiǎng)h除,但是你非得刪除,也可以象刪除文件那樣刪除它。
PS C:\test> Test-Path variable:value1
True
PS C:\test> del variable:value1
PS C:\test> Test-Path variable:value1
False
使用專用的變量命令
為了管理變量,powershell提供了五個(gè)專門(mén)管理變量的命令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也可以獲取這些附加信息。
變量寫(xiě)保護(hù)
可以使用New-Variable 的option選項(xiàng) 在創(chuàng)建變量時(shí),給變量加上只讀屬性,這樣就不能給變量重新賦值了。
PS C:\test> New-Variable num -Value 100 -Force -Option readonly
PS C:\test> $num=101
Cannot 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:num
Remove-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 -Force
PS C:\test> $num=101
PS C:\test> $num
101
有沒(méi)有權(quán)限更高的變量,有,那就是:選項(xiàng)Constant,常量一旦聲明,不可修改
PS C:\test> new-variable num -Value "strong" -Option constant
PS 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 -Force
Remove-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::name
PSDrive : Variable
PSProvider : Microsoft.PowerShell.CoreVariable
PSIsContainer : False
Name : name
Description : This is my name
Value : me
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}
您可能感興趣的文章:- Windows Powershell 自動(dòng)化變量
- Windows Powershell 環(huán)境變量
- Windows Powershell 變量的作用域
- Windows Powershell 變量的類型和強(qiáng)類型
- Windows Powershell 變量的幕后管理