麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PowerShell > 正文

Windows Powershell ForEach-Object 循環

2020-03-26 19:15:33
字體:
來源:轉載
供稿:網友

對管道對象逐個處理

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本免费一区二区三区四区 | 午夜色视频在线观看 | 特级毛片a级毛片100免费 | 蜜桃91丨九色丨蝌蚪91桃色 | 国产精品啪 | 久久久久久久久成人 | 久久草在线视频免费 | 日本羞羞的午夜电视剧 | 久久久精品视频免费看 | 欧美性受xxxxxx黑人xyx性爽 | 在线成人免费av | 日韩精品中文字幕一区二区三区 | 欧美日本91精品久久久久 | 综合精品 | 黄色av片三级三级三级免费看 | 国产精品99久久久久久宅女 | 日本在线视频一区二区三区 | 在线成人一区 | 国产成人小视频在线观看 | 久久草草影视免费网 | 国产欧美在线观看不卡一 | 全黄毛片| 久草视频福利在线观看 | 久久福利精品 | 亚洲第五色综合网 | 九草网 | 国产激情精品一区二区三区 | 91成人一区二区三区 | 九九视频精品在线 | 日韩精品网站在线观看 | 日本不卡一区二区在线观看 | 九九热精品在线 | 国产免费一区二区三区在线能观看 | 欧美中文字幕一区二区 | 日韩激情在线视频 | 黄色电影免费网址 | 精品人人人人 | 最近中文字幕一区二区 | 日韩欧美动作影片 | 午夜在线视频一区二区三区 | 免费看a级片 |