Windows Powershell 命令返回?cái)?shù)組
當(dāng)我們把一個(gè)命令的執(zhí)行結(jié)果保存到一個(gè)變量中,可能會(huì)認(rèn)為變量存放的是純文本。
但是,事實(shí)上Powershell會(huì)把文本按每一行作為元素存為數(shù)組。如果一個(gè)命令的返回值不止一個(gè)結(jié)果時(shí),Powershell也會(huì)自動(dòng)把結(jié)果存儲(chǔ)為數(shù)組。
PS C:Powershell> $IPcfg=ipconfig PS C:Powershell> $IPcfg Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : *** Link-local IPv6 Address . . . . . : *** IPv4 Address. . . . . . . . . . . : 192.168.140.128 Subnet Mask . . . . . . . . . . . : 255.255.252.0 Default Gateway . . . . . . . . . : 192.168.140.1 Tunnel adapter isatap.mossfly.com: Connection-specific DNS Suffix . : *** Link-local IPv6 Address . . . . . : *** Default Gateway . . . . . . . . . :*** Tunnel adapter Teredo Tunneling Pseudo-Interface: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : PS C:Powershell> $IPcfg.Count 22
使用數(shù)組存儲(chǔ)結(jié)果
判斷一個(gè)變量是否為數(shù)組
PS C:Powershell> $ip=ipconfig PS C:Powershell> $ip -is [array] True PS C:Powershell> "abac" -is [array] False PS C:Powershell> $str="字符串" PS C:Powershell> $str.ToCharArray() -is [array] True
查看數(shù)組的元素個(gè)數(shù)用$array.Count屬性。訪問(wèn)第x個(gè)元素,使用$array[x-1],因?yàn)閿?shù)組是以0開(kāi)始索引的。
使用管道對(duì)數(shù)組進(jìn)一步處理
PS C:Powershell> ipconfig | Select-String "IP" Windows IP Configuration Link-local IPv6 Address . . . . . : *** IPv4 Address. . . . . . . . . . . : *** Link-local IPv6 Address . . . . . : ***
使用真實(shí)的對(duì)象操作
為什么不愿把IPconfig返回的結(jié)果稱為對(duì)象,因?yàn)樗皇钦嬲鼵mdlet命令,真正的Powershell命令返回的數(shù)組元素可不止一個(gè)字符串,它是一個(gè)內(nèi)容豐富的對(duì)象。
PS C:Powershell> ls Directory: C:Powershell Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2011/11/23 17:25 ABC d---- 2011/11/29 18:21 myscript -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/29 19:23 21466 function.ps1 -a--- 2011/11/28 11:12 186 LogoTestConfig.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/30 16:04 2556 psdrive.html -a--- 2011/12/2 18:47 140 test.ps1 -a--- 2011/11/23 17:37 242 test.txt -a--- 2011/11/28 16:42 170 test.vbs PS C:Powershell> $result=ls PS C:Powershell> $result.Count 20
數(shù)組的每一個(gè)元素存放的是一個(gè)System.IO.DirectoryInfo對(duì)象。
當(dāng)我們輸出這些對(duì)象時(shí),Powershell會(huì)自動(dòng)幫我們把它轉(zhuǎn)換成友好的文本格式。
PS C:Powershell> $result[0].gettype().fullname System.IO.DirectoryInfo PS C:Powershell> $result[0] Directory: C:Powershell Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2011/11/23 17:25 ABC對(duì)于任何一個(gè)對(duì)象都可以使用Format-List * 查看它所有的屬性和方法。 PS C:Powershell> $result[0] | fl * PSPath : Microsoft.PowerShell.CoreFileSystem::C:PowershellABC PSParentPath : Microsoft.PowerShell.CoreFileSystem::C:Powershell PSChildName : ABC PSDrive : C PSProvider : Microsoft.PowerShell.CoreFileSystem PSIsContainer : True BaseName : ABC Mode : d---- Name : ABC Parent : Powershell Exists : True Root : C: FullName : C:PowershellABC Extension : CreationTime : 2011/11/23 17:25:53 CreationTimeUtc : 2011/11/23 9:25:53 LastAccessTime : 2011/11/23 17:25:53 LastAccessTimeUtc : 2011/11/23 9:25:53 LastWriteTime : 2011/11/23 17:25:53 LastWriteTimeUtc : 2011/11/23 9:25:53 Attributes : Directory
相關(guān)文章
PowerShell實(shí)現(xiàn)按條件終止管道的方法
這篇文章主要介紹了PowerShell實(shí)現(xiàn)按條件終止管道的方法,有時(shí)你可能想在管道運(yùn)行在某個(gè)特定的條件下,終止管道的運(yùn)行,本文就講解了這樣一種方法,需要的朋友可以參考下2015-06-06PowerShell腳本開(kāi)發(fā)之收發(fā)UDP消息包
上篇文章我們介紹了使用PowerShell收發(fā)TCP消息包,今天我們來(lái)介紹下使用Powershell收發(fā)UDP小細(xì)胞的方法2014-10-10PowerShell實(shí)現(xiàn)參數(shù)互斥示例
這篇文章主要介紹了PowerShell實(shí)現(xiàn)參數(shù)互斥示例,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06Powershell使用OpenFileDialog打開(kāi)文件示例
這篇文章主要介紹了Powershell使用OpenFileDialog打開(kāi)文件示例,本文直接給出示例代碼,需要的朋友可以參考下2015-03-03PowerShell中使用正則表達(dá)式跨行匹配字符串的方法
這篇文章主要介紹了PowerShell中使用正則表達(dá)式跨行匹配字符串的方法,重點(diǎn)在于正則表達(dá)式的寫(xiě)法,需要的朋友可以參考下2014-08-08Powershell的break、continue和return簡(jiǎn)單總結(jié)
這篇文章主要介紹了Powershell的break、continue和return簡(jiǎn)單總結(jié),和其它語(yǔ)言中的作用相同,所以本文的總結(jié)比較短小,需要的朋友可以參考下2014-07-07Powershell從注冊(cè)表中查詢默認(rèn)MAPI客戶端的例子
這篇文章主要介紹了Powershell從注冊(cè)表中查詢默認(rèn)MAPI客戶端的例子2014-05-05