Windows Powershell ForEach-Object 循环

所属分类: 脚本专栏 / PowerShell 阅读数: 707
收藏 0 赞 0 分享

对管道对象逐个处理

如果使用Get-WmiObject 获取系统中的服务,为了排版可能会也会使用Format-Table对结果进行表格排版。

复制代码 代码如下:

PS C:Powershell> Get-WmiObject Win32_Service | Format-Table status,DisplayName
-AutoSize

status DisplayName
------ -----------
OK     Adobe Acrobat Update Service
OK     Application Experience
OK     Application Layer Gateway Service
OK     Application Host Helper Service
OK     Application Identity
OK     Application Information
OK     Application Management
OK     ASP.NET State Service

但是如果想对每个服务进行更定制化的处理可是使用ForEach-Object

复制代码 代码如下:

PS C:Powershell> Get-WmiObject Win32_Service | ForEach-Object {"Name:"+ $_.Disp
layName, ", Is ProcessId more than 100:" + ($_.ProcessId -gt 100)}
Name:Adobe Acrobat Update Service , Is ProcessId more than 100:True
Name:Application Experience , Is ProcessId more than 100:False
Name:Application Layer Gateway Service , Is ProcessId more than 100:False
Name:Application Host Helper Service , Is ProcessId more than 100:True
Name:Application Identity , Is ProcessId more than 100:True
Name:Application Information , Is ProcessId more than 100:True
Name:Application Management , Is ProcessId more than 100:False
Name:ASP.NET State Service , Is ProcessId more than 100:False

结合条件处理

ForEach-Object的处理可以包含任意Powershell脚本,当然也包括条件语句

复制代码 代码如下:

Get-WmiObject Win32_Service | ForEach-Object {
    if ($_.ProcessId -gt 3000)
    { "{0}({1})" -f $_.DisplayName,$_.ProcessID}
}
Windows Presentation Foundation Font Cache 3.0.0.0(5408)
Microsoft Network Inspection(5260)
BranchCache(4112)
Windows Modules Installer(7656)

调用方法

在ForEach-Object中,$_代表当前对象,当然也允许通过$_,调用该对象支持的方法。
下面的例子杀死所有IE浏览器进程:

复制代码 代码如下:

PS C:Powershell> Get-Process iexplore

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    883      29    14728      22432   181    34.26   4300 iexplore
    771      28    55552     129152   425     8.56   5732 iexplore
   1216      51   104324     143916   539   572.41   5912 iexplore
    801      25    49200      25372   285     5.99   6252 iexplore
    691      25    57564      95796   333     8.08   6388 iexplore
   1256      38    85848     127012   379    20.37   7856 iexplore

PS C:Powershell> Get-Process iexplore | ForEach-Object {$_.kill()}
PS C:Powershell> Get-Process iexplore
Get-Process : 找不到名为“iexplore”的进程。请验证该进程名称,然后再次调用 cmdlet。
所在位置 行:1 字符: 12
+ Get-Process <<<< iexplore
+ CategoryInfo : ObjectNotFound: (iexplore:String) [Get-Process],
ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.
Commands.GetProcessCommand

更多精彩内容其他人还在看

使用HTTP api简单的远程执行PowerShell脚本

为了你能非常简单的远程执行PoweShell脚本,使用REST API是一个很好的选择,因为现在许多流行的编程语言都可以简单的执行HTTP的GET操作。
收藏 0 赞 0 分享

Powershell小技巧之使用WS-Man来调用PowerShell命令

大多Windows系统的管理员应当已经意识到在Windows系统上进行脚本开发和命令行管理,PowerShell首当其冲。微软许多产品和一些第三方产品都提供了Windows PowerShell的管理接口。但是目前PowerShell只能运行在Windows系统上,如何才能在非W
收藏 0 赞 0 分享

Windows Powershell For 循环

这篇文章主要介绍了Windows Powershell For 循环的定义、用法以及示例,非常简单实用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell Switch 循环

这篇文章主要介绍了Windows Powershell Switch 循环以及PowerShell中数组可以与switch语句结合,产生意想不到的效果。
收藏 0 赞 0 分享

Windows Powershell 定义函数

这篇文章主要介绍了Windows Powershell 定义函数,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell实现导入安装证书功能脚本分享

这篇文章主要介绍了Powershell实现导入安装证书功能脚本分享,本文用编程方法实现把pfx证书文件导入到指定的库中,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell改变脚本执行优先权的代码分享

这篇文章主要介绍了Powershell改变脚本执行优先权的代码分享,本文通过控制进程的方式调整程序的执行顺序,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell读取PFX证书并输入密码的脚本分享

这篇文章主要介绍了Powershell读取PFX证书并输入密码的脚本分享,本文实现直接用脚本输入密码,避免了手动输入密码的麻烦,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell访问SQL Server数据库代码实例

这篇文章主要介绍了Powershell访问SQL Server数据库代码实例,本文直接给出代码,使用时只需要替换数据库配置参数即可,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell打印文本文档例子

这篇文章主要介绍了Powershell打印文本文档例子,本文相对简单,默认给出的是打印系统日志文件,使用时要替换成自己想打印的文件,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多