2013-03-02 89 views
5

這可能嗎?Powershell - 安裝Windows更新?

我想我們需要調用WUAgent以某種方式運行檢測,但是我希望實質上下載並安裝更新,然後作爲腳本的一部分重新引導。

這將成爲一個更大的腳本的一部分,基本上建立一個香草2008R2框直到所有通過PowerShell的DC。

回答

2

我建議使用這個腳本

Function WSUSUpdate { 
$Criteria = "IsInstalled=0 and Type='Software'" 
$Searcher = New-Object -ComObject Microsoft.Update.Searcher 
try { 
    $SearchResult = $Searcher.Search($Criteria).Updates 
    if ($SearchResult.Count -eq 0) { 
     Write-Output "There are no applicable updates." 
     exit 
    } 
    else { 
     $Session = New-Object -ComObject Microsoft.Update.Session 
     $Downloader = $Session.CreateUpdateDownloader() 
     $Downloader.Updates = $SearchResult 
     $Downloader.Download() 
     $Installer = New-Object -ComObject Microsoft.Update.Installer 
     $Installer.Updates = $SearchResult 
     $Result = $Installer.Install() 
    } 
} 
catch { 
    Write-Output "There are no applicable updates." 
    } 
} 

WSUSUpdate 
If ($Result.rebootRequired) { Restart-Computer } 

來源:https://gist.github.com/jacobludriks/9ca9ce61de251a5476f1