powershell-管理windows update自动补丁更新

时间:2023-02-19 22:59:25

#需要使用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
#脚本2、扫描下载的补丁文件进行安装
for /f "delims=" %%a in ('dir /b /a-d "C:\Windows\SoftwareDistribution\Download\*.cab"') do start /wait wusa.exe "C:\Windows\SoftwareDistribution\Download\%%a" /quiet /norestart