You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell统计AD操作系统版本:需拆分输出为名称、版本、计数列

统计Active Directory操作系统版本数量并调整输出格式

需求:统计AD中操作系统版本数量,将当前合并在同一列的操作系统名称与版本拆分,输出名称、版本、计数三列。

当前输出格式

Name                                          Count
----                                          -----
Windows 10 Enterprise, 10.0 (19044)              26
Windows 10 Enterprise, 10.0 (18362)               1
Windows 10 Enterprise, 10.0 (19045)               8
Windows 10 Enterprise, 10.0 (18363)              11
Windows 10 Enterprise, 10.0 (19042)               1
Windows 8.1 Enterprise, 6.3 (9600)                2

现有实现代码

常规版本

$Computers = Get-ADComputer -Server XXX -Filter * -Properties OperatingSystem, OperatingSystemVersion -SearchBase 'OU=XXX,DC=xxxxxxx,DC=xxx'
        
$Results = ForEach ($Computer in $Computers){
    [PSCustomObject] @{     
        OperatingSystem = $Computer.OperatingSystem
        Version = $Computer.OperatingSystemVersion
    }
}

$Results | Group-Object -Property OperatingSystem, Version | Select-Object Name, Count

精简版本

Get-ADComputer -Server SHR -Filter * -Properties * -SearchBase 'OU=XXX,DC=xxxxxxx,DC=xxx' |
Group-Object -Property OperatingSystem,OperatingSystemVersion |
Select-Object Name,Count | Sort-Object Name

优化后的代码

通过计算属性从分组结果中提取单独的操作系统名称和版本字段,实现三列输出:

Get-ADComputer -Server XXX -Filter * -Properties OperatingSystem, OperatingSystemVersion -SearchBase 'OU=XXX,DC=xxxxxxx,DC=xxx' |
    Group-Object -Property OperatingSystem, OperatingSystemVersion |
    Select-Object @{Name='名称'; Expression={$_.Group[0].OperatingSystem}},
                  @{Name='版本'; Expression={$_.Group[0].OperatingSystemVersion}},
                  @{Name='计数'; Expression={$_.Count}} |
    Sort-Object 名称, 版本

期望输出格式

名称                  版本          计数
----                  ----          -----
Windows 10 Enterprise 10.0 (19044) 26
Windows 10 Enterprise 10.0 (18362) 1
Windows 10 Enterprise 10.0 (19045) 8
Windows 10 Enterprise 10.0 (18363) 11
Windows 10 Enterprise 10.0 (19042) 1
Windows 8.1 Enterprise 6.3 (9600)  2

内容的提问来源于stack exchange,提问作者RegularContrarian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:25:00