2017-08-16 172 views
3

這裏是我用於搜索WSUS安裝的Windows更新的代碼,我想爲重新啓動掛起/完成的狀態添加一列。有沒有開關?安裝Windows更新後檢查重新啓動狀態的Powershell

$Session = New-Object -ComObject "Microsoft.Update.Session" 

$Searcher = $Session.CreateUpdateSearcher() 

$historyCount = $Searcher.GetTotalHistoryCount() 

$Searcher.QueryHistory(0, $historyCount) | 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 
+0

我不認爲我們有任何檢查重啓狀態的特定開關。我認爲我們必須明確地檢查 –

回答

2

簡要介紹一下COM對象的屬性和方法並不顯示任何內容。 You can query update before to see if they might trigger a reboot但它不是客戶如何反應的保證。

可能還有其他方法,但是如果您想確切知道當前狀態,一個建議就是在註冊表中查找。

If a patch was installed by WindowsUpdates that requires a reboot it should leave a registry entry in this location

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired

所以你只需要檢查是否有在關鍵就吳關心的是它的掛起狀態的任何值。

$pendingRebootKey = "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" 
$results = (Get-Item $pendingRebootKey -ErrorAction SilentlyContinue).Property 

if($results){ 
    # Reboot is pending 
} 

使用-ErrorAction是因爲有用,根據文章:

注意, RebootRequired鍵被自動刪除當機器重新啓動,因爲它是 揮發性(僅保留在內存中)。

這可能隱藏其它潛在的問題,所以你可能需要將邏輯切換到一個try/catch,看的東西像ItemNotFoundException特定錯誤,如果有關注那裏。

+0

如何將此添加到我當前的輸出中作爲新列?一個新的對象? –

+0

這是在主機上執行正確嗎?您當前的輸出列表更新。儘管您可能能夠將更新連接到註冊表數據,但我不確定如何以令人滿意的方式爲您添加此更新。您可以添加另一個計算屬性,但它是運行計算機的屬性,而不是您正在查詢的更新。 – Matt