0

我有以下代码:

$versionList = "Windows 2000","Windows XP","Windows Vista","Windows 7","Windows 8","Windows Server 2003","Windows Server 2008","Windows Server 2008 R2","Windows Server 2012","Windows Server 2012 R2"

for($i = 0;$i -ne 10;$i++){
    $versionCheck = Get-ADComputer -Filter * -Property * | select OperatingSystem | where {$_.OperatingSystem -match $versionList[$i]}
       if($i -eq 0){
           Write-Host "Clients:" -for Green
       }
       if($i -eq 5){
           Write-Host "Servers:" -for Green
       }
    Write-Host $versionList[$i] `t $($versionCheck.OperatingSystem).Count 
}

这给出了以下输出:

Clients:
Windows 2000     0
Windows XP   0
Windows Vista    0
Windows 7    0
Windows 8    0
Servers:
Windows Server 2003      0
Windows Server 2008      0
Windows Server 2008 R2   0
Windows Server 2012      1
Windows Server 2012 R2   0

基于服务器的操作系统的数字排列得很好。客户不多。我通常会将其格式化为表格,但每次循环运行时都会更新变量,这会产生很多表格。我将如何正确格式化它?

4

1 回答 1

2

您可以使用这样的字符串格式化来格式化输出:

Write-Host ("{0,-22} {1,5}" -f $versionList[$i],$($versionCheck.OperatingSystem).Count)
于 2013-10-03T19:37:06.287 回答