• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Windows Powershell 命令返回数组

Windows Powershell 命令返回数组

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了windows powershell,windowspowershell2.0,windows powershell64,windows10 powershell,windows7 powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.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

</div>

使用数组存储结果
判断一个变量是否为数组

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
</div>

查看数组的元素个数用$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 . . . . . : ***
</div>

使用真实的对象操作

为什么不愿把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
</div>


数组的每一个元素存放的是一个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
</div>

</div>

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • Powershell 获取特定的网页信息的代码
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • 简单谈谈PowerShell 4.0中的新命令

相关文章

  • PowerShell小技巧之启动远程桌面连接
  • PowerShell小技巧之使用New-Module命令动态创建对象
  • PowerShell函数参数用星号隐藏的方法
  • PowerShell入门教程之PowerShell管道介绍
  • PowerShell脚本实现添加、修改任务计划的例子
  • PowerShell中使用Get-ChildItem命令读取目录、文件列表使用例子和小技巧
  • PowerShell快速创建一个指定大小文件的实例分享
  • PowerShell中的转义字符是什么?
  • 探索PowerShell(七) PowerShell变量
  • Windows Powershell Switch 语句

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell是什么?
    • Powershell中使用WMI工具例子
    • Powershell获取环境变量的方法
    • Powershell获取系统中所有可停止的服务
    • PowerShell中使用ArrayList实现数组插入、删除、添加例子
    • 探索PowerShell(六) 脚本基础简要
    • Powershell小技巧之设置IE代理
    • PowerShell脚本开发之对指定IP进行端口扫描
    • PowerShell函数中的开关参数介绍和创建实例
    • Powershell小技巧之系统运行时间

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有