Windows Powershell 命令返回數組_PowerShell

來源:互聯網
上載者:User

當我們把一個命令的執行結果儲存到一個變數中,可能會認為變數存放的是純文字。
但是,事實上Powershell會把文本按每一行作為元素存為數組。如果一個命令的傳回值不止一個結果時,Powershell也會自動把結果儲存為數組。

PS C:Powershell> $IPcfg=ipconfigPS C:Powershell> $IPcfgWindows IP ConfigurationEthernet 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.1Tunnel 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.Count22

使用數組儲存結果
判斷一個變數是否為數組

PS C:Powershell> $ip=ipconfigPS C:Powershell> $ip -is [array]TruePS C:Powershell> "abac" -is [array]FalsePS C:Powershell> $str="字串"PS C:Powershell> $str.ToCharArray() -is [array]True

查看數組的元素個數用$array.Count屬性。訪問第x個元素,使用$array[x-1],因為數組是以0開始索引的。

使用管道對數組進一步處理

PS C:Powershell> ipconfig | Select-String "IP"Windows IP Configuration  Link-local IPv6 Address . . . . . : ***  IPv4 Address. . . . . . . . . . . : ***  Link-local IPv6 Address . . . . . : ***

使用真實的對象操作

為什麼不願把IPconfig返回的結果稱為對象,因為它不是真正Cmdlet命令,真正的Powershell命令返回的數組元素可不止一個字串,它是一個內容豐富的對象。

PS C:Powershell> ls  Directory: C:PowershellMode        LastWriteTime   Length Name----        -------------   ------ ----d----    2011/11/23   17:25      ABCd----    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.vbsPS C:Powershell> $result=lsPS C:Powershell> $result.Count20


數組的每一個元素存放的是一個System.IO.DirectoryInfo對象。
當我們輸出這些對象時,Powershell會自動幫我們把它轉換成友好的文字格式設定。

PS C:Powershell> $result[0].gettype().fullnameSystem.IO.DirectoryInfoPS C:Powershell> $result[0]  Directory: C:PowershellMode        LastWriteTime   Length Name----        -------------   ------ ----d----    2011/11/23   17:25      ABC對於任何一個對象都可以使用Format-List * 查看它所有的屬性和方法。PS C:Powershell> $result[0] | fl *PSPath      : Microsoft.PowerShell.CoreFileSystem::C:PowershellABCPSParentPath   : Microsoft.PowerShell.CoreFileSystem::C:PowershellPSChildName    : ABCPSDrive      : CPSProvider    : Microsoft.PowerShell.CoreFileSystemPSIsContainer   : TrueBaseName     : ABCMode       : d----Name       : ABCParent      : PowershellExists      : TrueRoot       : C:FullName     : C:PowershellABCExtension     :CreationTime   : 2011/11/23 17:25:53CreationTimeUtc  : 2011/11/23 9:25:53LastAccessTime  : 2011/11/23 17:25:53LastAccessTimeUtc : 2011/11/23 9:25:53LastWriteTime   : 2011/11/23 17:25:53LastWriteTimeUtc : 2011/11/23 9:25:53Attributes    : Directory

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.