当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell 命令返回数组

Windows Powershell 命令返回数组

2017年12月08日  | 移动技术网IT编程  | 我要评论

当我们把一个命令的执行结果保存到一个变量中,可能会认为变量存放的是纯文本。
但是,事实上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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网