2017-08-30 22 views
0

如何將我的腳本的結果放在具有良好格式的同一頁上?當前ifelse被輸出到控制檯和Out-GridView如何在IF Else的同一頁上輸出結果?

$PCList = "C:\Scripts\Get-ADComputers\Win7.txt" 

foreach ($PC in Get-Content $PCList) { 
    if (-not(Test-Connection -ComputerName $pc -BufferSize 16 -Count 2 -Quiet)) { 
     Write-Host "$pc is not reachable" -ForegroundColor "yellow" 
    } else { 
     Invoke-Command -ComputerName $pc -ScriptBlock { 
      $Session = New-Object -ComObject "Microsoft.Update.Session" 
      $Searcher = $Session.CreateUpdateSearcher() 
      $historyCount = $Searcher.GetTotalHistoryCount() 
      $Searcher.QueryHistory(0, $historyCount) | 
       Where-Object {$_.title -like "*KB4019264*"} | 
       Select-Object Date, 
        @{name="Operation"; expression={switch($_.operation){1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}, 
        @{name="Status"; expression={switch($_.resultcode){1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 4 {"Failed"}; 5 {"Aborted"}}}}, 
        Title 
     } | Out-GridView 
    } 
} 

回答

2

你的意思是你想在gridview中的所有計算機的結果?然後,您需要使用與成功查詢產生的相同屬性爲失敗的連接創建對象。我建議將foreach循環更改爲ForEach-Object循環。您可能還需要將主機名字段添加到輸出中,以便您可以將一臺計算機與另一臺計算機區分開來。

Get-Content $PCList | ForEach-Object { 
    $pc = $_ 
    if (-not (Test-Connection -ComputerName $pc -BufferSize 16 -Count 2 -Quiet)) { 
     New-Object -Type PSObject -Property @{ 
      'Hostname' = $pc 
      'Date'  = $null 
      'Operation' = $null 
      'Status' = 'Connection failed' 
      'Title'  = $null 
     } 
    } else { 
     Invoke-Command -ComputerName $pc -ScriptBlock { 
      ... 
     } 
    } 
} | Out-GridView 
+0

我有超過200名在'$ pclist',之後我運行你的建議的腳本,它只返回我身邊60的結果,包括「平能」和「未平,能夠」主人,爲什麼它'$ pclist'中的主機其餘部分缺少結果? –

+0

很可能這些主機的歷史記錄中沒有修復程序KB4019264。如果您希望它們顯示爲「未安裝」,則需要檢查管道是否返回了結果並以其他方式創建自定義對象。 –