批量Windows更新脚本卡在最新累积更新安装环节无法结束如何解决
问题根因
PSWindowsUpdate 默认调用的更新安装逻辑未匹配累积更新的特殊安装要求:累积更新体积大、依赖服务堆栈更新(SSU)、安装过程中会触发内核态组件替换、需要重启才能完成后续流程,图形界面会自动处理安装等待、预启动环境配置等步骤,默认参数的命令行调用缺少对应配置才会无限挂起。
修复方案
- 首先调整
Install-WindowsUpdate的调用参数,补充累积更新所需的强制安装、自动处理重启、跳过用户交互的配置,注意必须优先安装服务堆栈更新(累积更新的强制前置依赖),替换原脚本最后一行的安装命令为:
# 优先安装服务堆栈更新、关键补丁类前置依赖 Install-WindowsUpdate -AcceptAll -Install -Confirm:$False -ForceInstall -Category "Service Packs","Critical Updates","UpdateRollup" -IgnoreReboot # 单独处理累积更新,允许自动重启完成安装流程 Install-WindowsUpdate -AcceptAll -Install -Confirm:$False -ForceInstall -AutoReboot -Verbose
- 新增更新缓存清理步骤,放在导入PSWindowsUpdate模块之后、执行安装之前,避免缓存损坏导致的安装卡滞:
# 停止Windows更新相关服务 Stop-Service wuauserv, cryptSvc, bits, msiserver -Force -ErrorAction SilentlyContinue # 清理旧更新缓存 Remove-Item -Path "C:\Windows\SoftwareDistribution\*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "C:\Windows\System32\catroot2\*" -Recurse -Force -ErrorAction SilentlyContinue # 重启服务 Start-Service wuauserv, cryptSvc, bits, msiserver -ErrorAction SilentlyContinue
- 如果仍然存在挂起问题,可新增超时检测逻辑,避免脚本无限等待,以下代码可放在安装命令之后:
# 配置最长等待时间为120分钟,超过自动结束安装进程触发重启 $installTimeout = New-TimeSpan -Minutes 120 $process = Get-Process TiWorker -ErrorAction SilentlyContinue if ($process) { $process.WaitForExit($installTimeout.TotalMilliseconds) if (!$process.HasExited) { Stop-Process $process -Force Restart-Computer -Force } }
内容的提问来源于stack exchange,提问作者Dario Corrada
相关产品推荐
相关产品推荐

