神剑山庄资源网 Design By www.hcban.com
当我们把一个命令的执行结果保存到一个变量中,可能会认为变量存放的是纯文本。
但是,事实上Powershell会把文本按每一行作为元素存为数组。如果一个命令的返回值不止一个结果时,Powershell也会自动把结果存储为数组。
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
使用数组存储结果
判断一个变量是否为数组
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
查看数组的元素个数用$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: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
数组的每一个元素存放的是一个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
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Windows Powershell 命令返回数组的评论...
更新日志
2024年10月04日
2024年10月04日
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】