一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - PowerShell - Windows Powershell ForEach-Object 循環

Windows Powershell ForEach-Object 循環

2020-06-24 10:20Powershell教程網 PowerShell

Powershell管道就像流水線,對于數據的處理是一個環節接著一個環節,如果你想在某一環節對流進來的數據逐個細致化的處理,可是使用ForEach-Object,$_ 代表當前的數據。

對管道對象逐個處理

如果使用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

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无删减影视免费观看 | 国产精品一二区 | 翁公与小莹在客厅激情 | 99精品国产自在现线观看 | 国产香蕉一区二区在线观看 | 亚洲欧洲色图 | 视频一区国产精戏刘婷30 | 午夜一区二区免费视频 | 亚洲国产在线观看免费视频 | 精品国产无限资源免费观看 | 亚洲欧美日韩国产精品影院 | 无码日韩精品一区二区免费 | 免费高清资源黄网站在线观看 | 白丝女榨干蹂躏我 | 精品国产一级在线观看 | 亚洲一区二区精品视频 | 51午夜| 三级视频中文字幕 | 果冻传媒在线免费观看 | 免费国产成人α片 | 国产精品久久久久网站 | 黑人巨大videosjapan高清 黑人好大 | 日本欧美强乱视频在线 | 成人在线日韩 | 亚洲午夜精品久久久久久抢 | 九九365资源稳定资源站 | 国产九九视频在线观看 | 双性肉文高h | 高清视频大片免费观看 | 国产精品永久免费10000 | 欧美另类变态 | 色综合天天综合网站中国 | 日韩欧美一区二区三区免费看 | 黄动漫车车好快的车车双女主 | 国内精品视频一区二区三区 | 双子母性本能在线观看 | 亚洲、国产综合视频 | 午夜在线观看免费观看 视频 | 17个农民工婉莹第一部 | 亚洲精品m在线观看 | 久久伊人免费 |