对管道对象逐个处理 如果使用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)