#需要使用powershell5.0以上版本
Install-Module PSWindowsUpdate
# 检查管理员权限
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "请以管理员身份运行此脚本。" -ForegroundColor Red
Exit 1
}
# 搜索并安装安全更新
Write-Host "搜索并安装安全更新..."
$updates = Get-WindowsUpdate -Category SecurityUpdates
foreach ($update in $updates) {
$title = $update.Title
Write-Host "安装更新: $title"
Install-WindowsUpdate -KBArticleID $update.KnowledgebaseArticleID
}
# 结束脚本
Write-Host "安装完成。"
Exit 0