当我们把一个命令的执行结果保存到一个变量中,可能会认为变量存放的是纯文本。

但是,事实上Powershell会把文本按每一行作为元素存为数组。如果一个命令的返回值不止一个结果时,Powershell也会自动把结果存储为数组。

  1. PS C:Powershell> $IPcfg=ipconfig
  2. PS C:Powershell> $IPcfg
  3. Windows IP Configuration
  4. Ethernet adapter Local Area Connection:
  5. Connection-specific DNS Suffix . : ***
  6. Link-local IPv6 Address . . . . . : ***
  7. IPv4 Address. . . . . . . . . . . : 192.168.140.128
  8. Subnet Mask . . . . . . . . . . . : 255.255.252.0
  9. Default Gateway . . . . . . . . . : 192.168.140.1
  10. Tunnel adapter isatap.mossfly.com:
  11. Connection-specific DNS Suffix . : ***
  12. Link-local IPv6 Address . . . . . : ***
  13. Default Gateway . . . . . . . . . :***
  14. Tunnel adapter Teredo Tunneling Pseudo-Interface:
  15. Media State . . . . . . . . . . . : Media disconnected
  16. Connection-specific DNS Suffix . :
  17. PS C:Powershell> $IPcfg.Count
  18. 22

使用数组存储结果

判断一个变量是否为数组

  1. PS C:Powershell> $ip=ipconfig
  2. PS C:Powershell> $ip -is [array]
  3. True
  4. PS C:Powershell> "abac" -is [array]
  5. False
  6. PS C:Powershell> $str="字符串"
  7. PS C:Powershell> $str.ToCharArray() -is [array]
  8. True

查看数组的元素个数用$array.Count属性。访问第x个元素,使用$array[x-1],因为数组是以0开始索引的。

使用管道对数组进一步处理

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

使用真实的对象操作

为什么不愿把IPconfig返回的结果称为对象,因为它不是真正Cmdlet命令,真正的Powershell命令返回的数组元素可不止一个字符串,它是一个内容丰富的对象。

  1. PS C:Powershell> ls
  2. Directory: C:Powershell
  3. Mode LastWriteTime Length Name
  4. ---- ------------- ------ ----
  5. d---- 2011/11/23 17:25 ABC
  6. d---- 2011/11/29 18:21 myscript
  7. -a--- 2011/11/24 18:30 67580 a.html
  8. -a--- 2011/11/24 20:04 26384 a.txt
  9. -a--- 2011/11/24 20:26 12060 alias
  10. -a--- 2011/11/24 20:27 12060 alias.ps1
  11. -a--- 2011/11/23 17:25 0 b.txt
  12. -a--- 2011/11/23 17:25 0 c.txt
  13. -a--- 2011/11/23 17:25 0 d.txt
  14. -a--- 2011/11/25 11:20 556 employee.xml
  15. -a--- 2011/11/29 19:23 21466 function.ps1
  16. -a--- 2011/11/28 11:12 186 LogoTestConfig.xml
  17. -a--- 2011/11/24 17:37 7420 name.html
  18. -a--- 2011/11/28 15:30 63 ping.bat
  19. -a--- 2011/11/24 17:44 735892 Powershell_Cmdlets.html
  20. -a--- 2011/11/30 16:04 2556 psdrive.html
  21. -a--- 2011/12/2 18:47 140 test.ps1
  22. -a--- 2011/11/23 17:37 242 test.txt
  23. -a--- 2011/11/28 16:42 170 test.vbs
  24. PS C:Powershell> $result=ls
  25. PS C:Powershell> $result.Count
  26. 20

数组的每一个元素存放的是一个System.IO.DirectoryInfo对象。

当我们输出这些对象时,Powershell会自动帮我们把它转换成友好的文本格式。

  1. PS C:Powershell> $result[0].gettype().fullname
  2. System.IO.DirectoryInfo
  3. PS C:Powershell> $result[0]
  4. Directory: C:Powershell
  5. Mode LastWriteTime Length Name
  6. ---- ------------- ------ ----
  7. d---- 2011/11/23 17:25 ABC

对于任何一个对象都可以使用 Format-List * 查看它所有的属性和方法。

  1. PS C:Powershell> $result[0] | fl *
  2. PSPath : Microsoft.PowerShell.CoreFileSystem::C:PowershellABC
  3. PSParentPath : Microsoft.PowerShell.CoreFileSystem::C:Powershell
  4. PSChildName : ABC
  5. PSDrive : C
  6. PSProvider : Microsoft.PowerShell.CoreFileSystem
  7. PSIsContainer : True
  8. BaseName : ABC
  9. Mode : d----
  10. Name : ABC
  11. Parent : Powershell
  12. Exists : True
  13. Root : C:
  14. FullName : C:PowershellABC
  15. Extension :
  16. CreationTime : 2011/11/23 17:25:53
  17. CreationTimeUtc : 2011/11/23 9:25:53
  18. LastAccessTime : 2011/11/23 17:25:53
  19. LastAccessTimeUtc : 2011/11/23 9:25:53
  20. LastWriteTime : 2011/11/23 17:25:53
  21. LastWriteTimeUtc : 2011/11/23 9:25:53
  22. Attributes : Directory