如何在 PowerShell 中基於多個條件引數獲取服務?


要過濾掉啟動型別為 “自動”並且狀態為 “已停止” 的服務,我們需要使用 -AND 比較運算子。在此,僅當兩個條件都匹配時,才會顯示服務。

命令

Get-Service | where{($_.StartType -eq "Automatic") -and ($_.Status -eq "Stopped")} |
Select Name, StartType, Status

輸出

Name       StartType  Status
----       ---------  ------
gpsvc      Automatic Stopped
gupdate    Automatic Stopped
MapsBroker Automatic Stopped

命令

要獲取啟動型別為 手動或已停用 的服務,將使用 -OR 運算子。

Get-Service | where{($_.StartType -eq "Manual") -or ($_.StartType -eq "Disabled")} |
Sort-Object Starttype | Select Name, StartType, Status

輸出

LxpSvc                                                    Manual Stopped
lmhosts                                                   Manual Running
KtmRm                                                     Manual Stopped
IpxlatCfgSvc                                              Manual Stopped
FontCache3.0.0.0                                          Manual Running
KeyIso                                                    Manual Running
klvssbridge64_20.0                                        Manual Stopped
UevAgentService                                         Disabled Stopped
tzautoupdate                                            Disabled Stopped
NetTcpPortSharing                                       Disabled Stopped
ssh-agent                                               Disabled Stopped
shpamsvc                                                Disabled Stopped
RemoteRegistry                                          Disabled Stopped
AppVClient                                              Disabled Stopped
svcdemo                                                 Disabled Stopped

更新日期: 2020 年 1 月 22 日

533 次觀看

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.