Powershell - 安装Windows更新?
问题描述:
这可能吗?Powershell - 安装Windows更新?
我想我们需要调用WUAgent以某种方式运行检测,但是我希望实质上下载并安装更新,然后作为脚本的一部分重新引导。
这将成为一个更大的脚本的一部分,基本上建立一个香草2008R2框直到所有通过PowerShell的DC。
答
我建议使用这个脚本
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