PowerShell 实用技能与对象操作指南
1. 简化 Foreach - Object 管道操作
在 PowerShell 中, Foreach - Object 命令非常强大,它能让我们访问管道对象的方法和属性。不过,在简单场景下,基于脚本块的语法可能会显得有些繁琐。例如:
Get - Process | Foreach - Object { $_.Name }
在 PowerShell 3 版本中, Foreach - Object 命令(及其别名 % )得到了扩展,极大地简化了属性和方法的访问:
Get - Process | Foreach - Object Name
Get - Process | % Name | % ToUpper
此外,PowerShell 语言还提供了一种快速枚举属性的方法。就像我们可以访问单个元素的属性一样,也能用类似的语法访问集合中每个元素的属性:
PS > Start - Process PowerShell
PS > Start - Process PowerShell
PS > $processes = Get - Process - Name PowerShell
PS > $processes[0].Id
7928
超级会员免费看
订阅专栏 解锁全文
61

被折叠的 条评论
为什么被折叠?



