如何在 PowerShell 中根据多个条件参数获取服务?
powershellmicrosoft technologiessoftware & coding
要筛选出启动类型为"自动"且状态为"已停止"的服务,我们需要使用-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

