Windows Powershell ForEach-Object 循環(huán)
對(duì)管道對(duì)象逐個(gè)處理
如果使用Get-WmiObject 獲取系統(tǒng)中的服務(wù),為了排版可能會(huì)也會(huì)使用Format-Table對(duì)結(jié)果進(jìn)行表格排版。
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
但是如果想對(duì)每個(gè)服務(wù)進(jìn)行更定制化的處理可是使用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
結(jié)合條件處理
ForEach-Object的處理可以包含任意Powershell腳本,當(dāng)然也包括條件語(yǔ)句
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)
調(diào)用方法
在ForEach-Object中,$_代表當(dāng)前對(duì)象,當(dāng)然也允許通過(guò)$_,調(diào)用該對(duì)象支持的方法。
下面的例子殺死所有IE瀏覽器進(jìn)程:
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”的進(jìn)程。請(qǐng)驗(yàn)證該進(jìn)程名稱,然后再次調(diào)用 cmdlet。
所在位置 行:1 字符: 12
+ Get-Process <<<< iexplore
+ CategoryInfo : ObjectNotFound: (iexplore:String) [Get-Process],
ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.
Commands.GetProcessCommand
相關(guān)文章
Powershell學(xué)習(xí)筆記--使用正則表達(dá)式查找文件
本文介紹PowerShell中使用正則表達(dá)式的查找文件的方法,PowerShell的正則表達(dá)式與微軟其它語(yǔ)言的正則表達(dá)式是一樣的,使用非常方便。2014-08-08PowerShell中執(zhí)行Javascript的方法示例
這篇文章主要介紹了PowerShell中執(zhí)行Javascript的方法示例,特殊場(chǎng)景下可能會(huì)用到這個(gè)技巧,需要的朋友可以參考下2014-07-07PowerShell中的TimeSpan時(shí)間差對(duì)象使用實(shí)例
這篇文章主要介紹了PowerShell中的TimeSpan對(duì)象使用實(shí)例,TimeSpan對(duì)象表示一個(gè)時(shí)間差,本文介紹了多種獲取時(shí)間差的方法,需要的朋友可以參考下2014-08-08PowerShell實(shí)現(xiàn)批量重命名文件
這篇文章主要介紹了PowerShell實(shí)現(xiàn)批量重命名文件,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04基于PowerShell在Ubuntu系統(tǒng)的使用詳解
下面小編就為大家分享一篇基于PowerShell在Ubuntu系統(tǒng)的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02PowerShell入門(mén)教程之PowerShell有什么用?
這篇文章主要介紹了PowerShell入門(mén)教程之PowerShell有什么用?本文講解了PowerShell的三大作用,需要的朋友可以參考下2014-10-10PowerShell入門(mén)教程之快速學(xué)習(xí)PowerShell的幾個(gè)方法
這篇文章主要介紹了PowerShell入門(mén)教程之快速學(xué)習(xí)PowerShell的幾個(gè)方法,本文提出了3種快速學(xué)習(xí)PowerShell的方法,需要的朋友可以參考下2014-10-10PowerShell中按文件后綴過(guò)濾的實(shí)現(xiàn)代碼
這篇文章主要介紹了PowerShell中按文件后綴過(guò)濾的實(shí)現(xiàn)代碼,自定義了一個(gè)過(guò)濾器來(lái)滿足需求,需要的朋友可以參考下2014-08-08