PowerShell - 计算 AD 中的操作系统版本

问题描述 投票:0回答:1

所以我想计算 AD 中操作系统版本的数量。我已经完成了这个工作,但对输出并不完全满意。问题是 OperatingSystem 和 OperatingSystemVersion 合并到同一列中。理想情况下,我想要三个标头:名称、版本、计数。

我得到的是:

姓名数
---- -----
Windows 10 企业版,10.0 (19044) 26
Windows 10 企业版,10.0 (18362) 1
Windows 10 企业版,10.0 (19045) 8
Windows 10 企业版,10.0 (18363) 11
Windows 10 企业版,10.0 (19042) 1
Windows 8.1 企业版、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

我想要得到类似的东西:

名称版本计数

powershell active-directory
1个回答
0
投票

从每组中的第一个元素获取值:

$Groups = $Results | Group-Object -Property OperatingSystem, Version |Sort Name
$Groups |Select-Object @{Name='OperatingSystem';Expression={$_.Group[0].OperatingSystem}},@{Name='Version';Expression={$_.Group[0].OperatingSystemVersion}}, Count
© www.soinside.com 2019 - 2024. All rights reserved.