Windows Powershell ForEach-Object 循环

所属分类: 脚本专栏 / PowerShell 阅读数: 646
收藏 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

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

PowerShell实现统计函数嵌套深度

这篇文章主要介绍了PowerShell实现统计函数嵌套深度,本文分享一个函数,可以实现统计脚本执行的嵌套层次,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell互斥参数使用实例

这篇文章主要介绍了Powershell互斥参数使用实例,本文给出了两个代码示例来讲解互斥参数的使用,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell实现按条件终止管道的方法

这篇文章主要介绍了PowerShell实现按条件终止管道的方法,有时你可能想在管道运行在某个特定的条件下,终止管道的运行,本文就讲解了这样一种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell Continue语句使用示例

这篇文章主要介绍了PowerShell Continue语句使用示例,本文直接给出示例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell实现动态获取当前脚本运行时消耗的内存

这篇文章主要介绍了PowerShell实现动态获取当前脚本运行时消耗的内存,本文直接给出实现脚本函数,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell实现参数互斥示例

这篇文章主要介绍了PowerShell实现参数互斥示例,本文直接给出示例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中使用.NET将程序集加入全局程序集缓存

这篇文章主要介绍了PowerShell中使用.NET将程序集加入全局程序集缓存,本文介绍了两种方法实现把程序集加入全局程序集缓存,着重讲解了使用.Net的类库解决这个需求,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中获取当前运行脚本路径的方法

这篇文章主要介绍了PowerShell中获取当前运行脚本路径的方法,获取方法很简单,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中的函数重载示例

这篇文章主要介绍了PowerShell中的函数重载示例,本文直接给出一个完整重载示例,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中以管理员权限启动应用程序的方法

这篇文章主要介绍了PowerShell中以管理员权限启动应用程序的方法,方法很简单,本文给出启动词本和powershell为例讲解如何用管理员身份启动软件,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多