在 PowerShell 中分組物件:整理您的資料

在使用 PowerShell 时,有时需要处理一组数据。我们需要处理很多文件、用户账户、虚拟机等等。当在控制台中显示这些不同的对象时,它们会一个接一个地滚动在屏幕上,而不考虑您想如何查看它们。但这是可以解决的!使用 PowerShell 的 Group-Object 命令,您可以将对象分组在一起。这个命令类似于 SQL GROUP BY 语句

PowerShell 通过 Powershell Group Object 命令提供了一种通过属性对对象进行汇总的方法。这个命令允许脚本编写者一次性以分组的方式查看大量对象属性。

让我们生成一堆相同类型的对象。这些对象可以是任何类型。然而,为了进行演示,我将使用 System.ServiceProcess.ServiceController 对象,这些对象是由 Get-Service 命令返回的。

为了让 Group-Object 按预期工作,请确保只对相同类型的对象进行分组。所有对象具有相同的属性是很重要的。

PS51> $services = Get-Service
PS51> $services

Status   Name               DisplayName
------   ----               -----------
Stopped  AdtAgent           Microsoft Monitoring Agent Audit Fo...
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
--snip--

使用 ManageEngine ADManager Plus 管理和报告 Active Directory、Exchange 和 Microsoft 365。 立即下载免费试用版!

不带参数的 Group-Object

Get-Service 返回本地计算机上的所有服务。由于这里有很多服务,我无法了解它们的状态、启动类型等。我想首先按照它们的状态对这些服务进行分组。为了将这些服务分组,我将所有服务对象传递给 PowerShell 的 Group-Object 命令,然后使用 Status 属性。

PS51> $services | Group-Object

Count Name                      Group
----- ----                      -----
  272 AdtAgent                  {AdtAgent, AJRouter, ALG, AppHostSvc...}

请注意,通过将所有服务传递给 Powershell Group Object,您可以获得计数。这可能看起来并不重要,但非常有用。

通过单个属性对对象进行分组

要按照特定属性(在本例中为状态)对它们进行分组,我需要告诉 Group-Object 命令我想要按照特定对象属性进行分组。这可以通过在 Group-Object 上使用 Property 参数来实现。

PS51> $services | Group-Object -Property Status

Count Name                      Grou
----- ----                      -----
  160 Stopped                   {AdtAgent, AJRouter, ALG, AppIDSvc...}
  112 Running                   {AppHostSvc, Appinfo, Appveyor.Server, AudioEndpointBuilder...}

现在我们开始了!我现在可以一次看到有多少个停止的服务和正在运行的服务。我也可以对 StartType 做同样的操作。

PS51> $services | Group-Object -Property StartType

Count Name                      Group
----- ----                      -----
    9 Disabled                  {AdtAgent, AppVClient, NetTcpPortSharing, RemoteAccess...}
  184 Manual                    {AJRouter, ALG, AppIDSvc, Appinfo...}
   79 Automatic                 {AppHostSvc, Appveyor.Server, AudioEndpointBuilder, Audiosrv...}

过滤 Group-Object 输出

也许我想深入了解并查看其中一个或多个组中的实际服务。我可以通过查看由 Powershell group object 命令返回的 Group 属性来获取这些对象。Group 对象包含所有具有分组对象属性值的服务。如果我正在按照服务状态进行分组,我可以通过筛选出状态为 Stopped 的服务,然后展开 Group 对象来查看所有这些服务。

PS51> $services | Group-Object -Property Status | Where {$_.Name -eq 'Stopped'} | Select -ExpandProperty Group

Status   Name               DisplayName
------   ----               -----------
Stopped  AdtAgent           Microsoft Monitoring Agent Audit Fo...
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
Stopped  AppMgmt            Application Management
Stopped  AppReadiness       App Readiness
--snip--

通过多个属性对对象进行分组

不僅可以在單個屬性上進行分組,還可以在多個屬性上進行分組。也許您想根據服務的狀態啟動類型來查看所有服務。為了這樣做,只需將另一個屬性名稱添加到 Group-Object Property 參數中。

PS51> $services | Group-Object -Property Status,StartType

Count Name                      Group
----- ----                      -----
    9 Stopped, Disabled         {AdtAgent, AppVClient, NetTcpPortSharing, RemoteAccess...}
  145 Stopped, Manual           {AJRouter, ALG, AppIDSvc, AppMgmt...}
   73 Running, Automatic        {AppHostSvc, Appveyor.Server, AudioEndpointBuilder, Audiosrv...}
   39 Running, Manual           {Appinfo, camsvc, CertPropSvc, ClipSVC...}
    6 Stopped, Automatic        {gpsvc, MapsBroker, sppsvc, TrustedInstaller...}

通過添加其他屬性,您可以創建許多“和”場景並在必要時按多個屬性進行分組!

使用ManageEngine ADManager Plus管理和報告Active Directory、Exchange和Microsoft 365。下載免費試用版!

摘要

Group-Object cmdlet是一個幫助您根據共同屬性將相似對象分組在一起的cmdlet。以這種方式分組對象在許多不同的情況下都非常方便。我希望通過了解 Group-Object cmdlet的工作方式,您可以獲得更多關於如何改進和創建更好的PowerShell腳本的想法!

如果您剛開始使用PowerShell,我強烈建議您查看我的PowerShell工具構建迷你課程!

Source:
https://adamtheautomator.com/powershell-group-object/