我尝试了几种收集服务数据的方法,但似乎无法找到一种满足所有需求的方法。Get-Service
工作得很好,但当我导入几个Where-Object
属性时,速度非常慢。Get-CimInstance
要快得多,但我不知道如何排除服务。有什么想法吗?
以下是我迄今为止的代码尝试。这个很快,直到我加上Where-Object
。如果我这样做,需要3倍的时间:
Get-Service -DisplayName * -ComputerName $Name -Exclude $ExcludedServices | Where-Object { $_.status -eq 'Running' -or $_.StartType -eq 'Automatic' }
这个工作速度快得多,但如果需要,我不知道如何排除服务列表:
Get-CimInstance -ClassName Win32_Service -ComputerName $Name | Where-Object { $_.state -eq 'Running' -or $_.StartMode -eq 'Auto' }
如果需要,我不知道如何排除服务列表
Get-CimInstance
允许您对查询施加WQL WHERE子句约束:
Get-CimInstance Win32_Service -Filter 'Name != "excludedSvc"'
您还可以根据查询中的State
或StartMode
属性限制项目,这样远程计算机就不必发回所有服务:
Get-CimInstance Win32_Service -Filter 'Name != "excludedSvc" AND State = "Running" AND StartMode = "Auto"'