麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PowerShell > 正文

Windows Powershell 命令返回?cái)?shù)組

2020-10-29 21:04:01
字體:
供稿:網(wǎng)友

當(dāng)我們把一個命令的執(zhí)行結(jié)果保存到一個變量中,可能會認(rèn)為變量存放的是純文本。
但是,事實(shí)上Powershell會把文本按每一行作為元素存為數(shù)組。如果一個命令的返回值不止一個結(jié)果時,Powershell也會自動把結(jié)果存儲為數(shù)組。

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

使用數(shù)組存儲結(jié)果
判斷一個變量是否為數(shù)組

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

查看數(shù)組的元素個數(shù)用$array.Count屬性。訪問第x個元素,使用$array[x-1],因?yàn)閿?shù)組是以0開始索引的。

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

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

使用真實(shí)的對象操作

為什么不愿把IPconfig返回的結(jié)果稱為對象,因?yàn)樗皇钦嬲鼵mdlet命令,真正的Powershell命令返回的數(shù)組元素可不止一個字符串,它是一個內(nèi)容豐富的對象。

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


數(shù)組的每一個元素存放的是一個System.IO.DirectoryInfo對象。
當(dāng)我們輸出這些對象時,Powershell會自動幫我們把它轉(zhuǎn)換成友好的文本格式。

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩高清电影 | 久久久久久久久久久久久国产精品 | 免费毛片视频播放 | 欧美三区在线 | 久久在草 | 久久不射电影 | 免费一级特黄毛片 | 亚洲一区在线免费视频 | gril hd| 色婷婷tv| sm高h视频 | 午夜激情视频免费 | 99国产精品欲a | 日韩视频中文 | 农村少妇吞精夜夜爽视频 | 动漫孕妇被羞羞视频 | 国产成年人视频网站 | 黄色电影免费网址 | 看免费av| 欧美一级毛片大片免费播放 | 欧美成年人视频在线观看 | 91精品国产777在线观看 | 成年人黄色免费电影 | 成人午夜在线免费观看 | 国产毛片毛片毛片 | 日韩视频一区二区三区在线观看 | 污污网站入口 | 性欧美在线视频 | 国产精品久久久久久久四虎电影 | 巨根插入 | 一色桃子av大全在线播放 | 香蕉国产片 | 12av毛片 | 九九热视频这里只有精品 | 欧美精品18 | 国产日韩欧美 | 欧美三级欧美成人高清www | 欧美高清第一页 | 宅男噜噜噜66国产在线观看 | 欧美一区二区三区久久精品视 | 91色成人|