顾名思义,Group-Object可用于对相似的属性进行分组。
Get-Service | Group-Object Status
输出结果
Count Name Group ----- ---- ----- 160 Stopped {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, AJR outer, ALG...} 130 Running {AdobeARMservice, Appinfo, AudioEndpointBuilder, Audiosrv...}
上面的输出按状态分组(“已停止”和“正在运行”)。共有160个服务处于“已停止”状态,而130个处于“运行”状态。
同样,您可以使用starttype属性过滤组。
Get-Service | Group-Object StartType
输出结果
PS C:\WINDOWS\system32> Get-Service | Group- Object StartType Count Name Group ----- ---- ----- 197 Manual {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, A JRouter, ALG...} 84 Automatic {AdobeARMservice, AudioEndpointBuilder, Audi , AVP20.0...} 9 Disabled {AppVClient, NetTcpPortSharing, RemoteAccess , RemoteRegistry...}
如果仅需要使用Count和Name属性,则-NoElement参数。
Get-Service | Group-Object StartType -NoElement
输出结果
Count Name ----- ---- 197 Manual 84 Automatic 9 Disabled
如果需要该组的值,则首先需要转换为HashTable,如下所示。
Get-Service | Group-Object Status –AsHashTable -AsString
输出结果
Name Value ---- ----- Stopped {AarSvc_8f3023, AdobeFlashPlayerUpdateSvc, AJRo uter, ALG...} Running {AdobeARMservice, Appinfo, AudioEndpointBuilder , Audiosrv...}
现在我们对正在运行的服务感兴趣,因此服务的输出将存储在变量中,然后将使用“运行”值检索所需的输出。
$services = Get-Service | Group-Object Status -AsHashTable -AsString $services.Running
输出结果
PS C:\WINDOWS\system32> $services.Running Status Name DisplayName ------ ---- ----------- Running AdobeARMservice Adobe Acrobat Update Service Running Appinfo Application Information Running AudioEndpointBu... Windows Audio Endpoint Builder Running Audiosrv Windows Audio Running AVP20.0 Kaspersky Anti-Virus Service 20.0 Running BFE Base Filtering Engine Running BITS Background Intelligent Transfer Ser... Running Bluetooth Devic... Bluetooth Device Monitor Running Bluetooth OBEX ... Bluetooth OBEX Service Running BrokerInfrastru... Background Tasks Infrastructure Ser... Running Browser Computer Browser Running BTAGService Bluetooth Audio Gateway Service Running BthAvctpSvc AVCTP service Running bthserv Bluetooth Support Service Running camsvc Capability Access Manager Service Running cbdhsvc_8f3023 Clipboard User Service_8f3023 Running CDPSvc Connected Devices Platform Service Running CDPUserSvc_8f3023 Connected Devices Platform User Ser... Running ClickToRunSvc Microsoft Office Click-to-Run Service Running ClipSVC Client License Service (ClipSVC)