格式化循环输出

时间:2013-10-03 19:10:17

标签: powershell

我有以下代码:

$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

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

1 个答案:

答案 0 :(得分:2)

您可以使用以下字符串格式设置输出格式:

Write-Host ("{0,-22} {1,5}" -f $versionList[$i],$($versionCheck.OperatingSystem).Count)