
(1)把以下代码保存为a.ps1并放到C盘根目录,以管理员运行打开PowerShell
(2)在窗口中输入Set-ExecutionPolicy RemoteSigned 按Y
(3)在窗口中输入c:\a.ps1
cls
function Remove-Update {
$TimeOutDays=7 #定义时间差
$hotfixes=(Get-HotFix | Sort-Object -Property installedon -Descending) #获取补丁列表集并按安装日期降序排列
foreach ($hotfix in $hotfixes) #循环集
{ $installtime=$hotfix | Select-Object -ExpandProperty installedon #取更新包安装日期
$daypan=((get-date)-$installtime).days #系统当前时间减去更新包的安装时间
if ($TimeOutDays -gt $daypan ) #定义时间差≥实际时间差
{
"Inside first if"
$KBID = $hotfix.HotfixId.Replace("KB", "") #取更新包的ID
$RemovalCommand = "wusa.exe /uninstall /kb:$KBID /quiet /norestart" #通过wusa.exe命令卸载更新包
Write-Host "卸载 KB$KBID ,安装时间:"$installtime #显示信息
Invoke-Expression $RemovalCommand
while (@(Get-Process wusa -ErrorAction SilentlyContinue).Count -ne 0) #查询进程wusa状态
{ Start-Sleep 3
Write-Host "wusa.exe正在运行 ...卸载中..." }
} }
}
Remove-Update