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
相关产品推荐
相关产品推荐

