主頁 > 知識庫 > Windows Powershell 命令返回數(shù)組

Windows Powershell 命令返回數(shù)組

熱門標簽:萬全縣地圖標注app 如何根據(jù)經(jīng)緯度百度地圖標注 電話機器人公司招聘 新鄭電銷機器人一個月多少錢 騰訊地圖標注中心怎么標注 六寸地圖標注點怎么刪除 莫拉克電梯系統(tǒng)外呼怎么設置 印臺區(qū)呼叫中心外呼系統(tǒng) 地圖標注的圖案

當我們把一個命令的執(zhí)行結果保存到一個變量中,可能會認為變量存放的是純文本。
但是,事實上Powershell會把文本按每一行作為元素存為數(shù)組。如果一個命令的返回值不止一個結果時,Powershell也會自動把結果存儲為數(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ù)組存儲結果
判斷一個變量是否為數(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ù)組的元素個數(shù)用$array.Count屬性。訪問第x個元素,使用$array[x-1],因為數(shù)組是以0開始索引的。

使用管道對數(shù)組進一步處理

PS C:Powershell> ipconfig | Select-String "IP"

Windows IP Configuration
  Link-local IPv6 Address . . . . . : ***
  IPv4 Address. . . . . . . . . . . : ***
  Link-local IPv6 Address . . . . . : ***

使用真實的對象操作

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

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ù)組的每一個元素存放的是一個System.IO.DirectoryInfo對象。
當我們輸出這些對象時,Powershell會自動幫我們把它轉換成友好的文本格式。

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對于任何一個對象都可以使用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

您可能感興趣的文章:
  • Windows Powershell 創(chuàng)建數(shù)組
  • Windows Powershell 訪問數(shù)組
  • Windows Powershell 復制數(shù)組
  • Windows Powershell強類型數(shù)組
  • Windows Powershell使用哈希表
  • Windows Powershell使用管道

標簽:疫苗接種 喀什 湘潭 襄陽 南昌 臨汾 天水 汕頭

巨人網(wǎng)絡通訊聲明:本文標題《Windows Powershell 命令返回數(shù)組》,本文關鍵詞  Windows,Powershell,命令,返回,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Windows Powershell 命令返回數(shù)組》相關的同類信息!
  • 本頁收集關于Windows Powershell 命令返回數(shù)組的相關信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章