国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PowerShell > 正文

Windows Powershell ForEach-Object 循環

2020-05-30 20:16:42
字體:
來源:轉載
供稿:網友

對管道對象逐個處理

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华池县| 博客| 六安市| 文山县| 长兴县| 防城港市| 惠水县| 循化| 赤城县| 平阳县| 九江县| 手游| 商都县| 涞水县| 普格县| 依安县| 奇台县| 三门峡市| 巴彦淖尔市| 获嘉县| 手游| 清徐县| 无锡市| 当阳市| 卢氏县| 贵溪市| 晋江市| 赣州市| 星座| 宣威市| 巴南区| 长沙县| 健康| 蛟河市| 轮台县| 三明市| 巴林右旗| 台安县| 康乐县| 合山市| 渑池县|