數組其實在任何語言裡都是一個比較頭疼的學習對象,在學習PowerShell的空余時間,做了以下簡單 整理:
1. 在PowerShell中,只要把不同的對象用逗號“,”連接起來,就可以構造出數組對象。
例如:520,'abc',2MB,0xFFFE
520 abc 2097152 65534
上面的小例子輸出了4個對象:數值“520”、字符串“abc”、數值“2MB”和十六進制數值“0xFFFE ”。
2. 關於數組對象更多的特性:
$array = 1,2,3,4,5 $int = 1 $array.count 5 "$array" 1 2 3 4 5 $int.count "$int" 1
以上$int是一個整數,而不是數組。
3. PowerShell是基於對象的shell,現在讓我們來看看數組究竟是什麼對象:
$array.GetType().FullName System.Object[]
其實Powershell實際操作的對象還是.Net類庫中的對象。使用數組時,需要大家記住數組的第一個元 素的下標是“0”、數組的最後一個元素下標是數組長度減去1。例如:
$array = 1,2,3 $array.Length 3 $array[0] 1 $array[2] 3
實際上,當數組訪問越界時,PowerShell是不會給出任何錯誤信息的,只會得到一個$null對象,如 $array[4] 。
3. 如何向這個數組中添加新的元素呢:
$a = " I ", " am " $a[0] I $a[1] am $a[2] = " Lihua " Array assignment failed because index '2' was out of range. At line:1 char:4 + $a[2 <<<< ]=" Lihua "
雖然如此,但PowerShell可以通過$a[0]="we"來修改數組元素值。另外添加則需要:
$a = $a + " Lihua " $a I am Lihua
最後補充一下學習PowerShell中的系統cmdlets技巧:
我寫了一個批量輸出幫助文件,並只輸出Examples的例子,希望對你學習有幫助。
$d = Get-Command -CommandType Cmdlet "Total:" + $d.Count $i = 0 $d | ForEach-Object -process{ $i++ "Printing" + $i $cmdlet = $_.Name $path = ".\EXAMPLE\"+$cmdlet + ".ps1" Get-Help -Examples $cmdlet > $path } "Successfully" #test
以下是執行上面腳本後輸出的結果:
然後我們打開其中一個文件看一下:
NAME
ForEach-Object
SYNOPSIS
Performs an operation against each of a set of input objects.
-------------------------- EXAMPLE 1 --------------------------
C:\PS>30000,56798,12432 | foreach-object -process {$_/1024}
This command accepts an array of integers, divides each one of them by 1024, and displays the r
esults.
-------------------------- EXAMPLE 2 --------------------------
C:\PS>get-childitem C:\ | foreach-object -process { $_.length / 1024 }
This command retrieves the files and directories in the root of the C: drive and returns and di
splays the size of each of them. The zeroes represent directories where no file size was availa
ble.
-------------------------- EXAMPLE 3 --------------------------
C:\PS>$events = get-eventlog -logname system -newest 1000
$events |
foreach-object -begin {get-date}
-process {out-file -filepath events.txt -append -inputobject $_.message}
-end {get-date}
This command retrieves the 1000 most recent events from the system log and stores them in the $
events variable. It then pipes the events to the ForEach-Object cmdlet. The Begin parameter dis
plays the current date and time. Next, the Process parameter uses the Out- File cmdlet to create
a text file named events.txt and stores the message property of each of the events in that fil
e. Last, the End parameter is used to display the date and time after all of the processing has
completed.
-------------------------- EXAMPLE 4 --------------------------
C:\PS>get-itemproperty -path hkcu:\Network\* |
foreach-object {set-itemproperty -path $_.pspath -name RemotePath
-value $_.RemotePath.ToUpper();}
This command updates a set of registry entries by using the Set-ItemProperty cmdlet. The regist
ry entries specify the UNC path of mapped network drives on the computer. This command changes
the characters in the UNC path to uppercase. In a real scenario, you might need to change the s
erver name or another component of the path.
Because the Set-ItemProperty cmdlet acts on a single property, the ForEach- Object cmdlet is use
d to call Set-ItemProperty for each property (registry entries in this case) being changed. The
name of the registry entries is RemotePath and they are located in HKCU\Network\<mapped drive
letter>. The entries are retrieved by using the Get-ItemProperty cmdlet and piped as input to F
orEach-Object. In the call to Set-ItemProperty within ForEach-Object, the $_ variable holds a r
eference to the current registry entry being processed. The value of the PSPath property is the
registry key containing the current entry. The RemotePath property is of type System.String, w
hich includes the ToUpper method used to change the value of the entry to uppercase.
你會發現這對我們學習陌生的cmdlets非常實用。 由於我的系統是英文的,Powershell也是英文版的, 所以例子與解說都是英文的。
另外,如果你需要對自定義的cmdlets作以上的輸出,而不需要輸出系統的cmdlets,可以做如下改進 :
#This is a patch output for only customized cmdlets $d = Get-Command -PSSnapin Zivsoft.PowerShell.Cmdlets "Total:" + $d.Count $i = 0 $d | ForEach-Object -process{ $i++ "Printing" + $i $cmdlet = $_.Name $path = ".\CmdletsUsage\"+$cmdlet + ".ps1" Get-Help -Examples $cmdlet > $path } "Successfully"
執行後,會將系統的過濾掉不輸出。
最後,細心人可以看出我的腳本還是有很多漏洞,比如直接用"successfully"判斷成功,其實不對的 ,但我想不到其它好的辦法,由於不影響大局,代碼沒做過多改進。