WSU 离线扫描:无网络环境安全补丁检测方案
🛡️ 安全运维
利用 wsusscn2.cab 离线数据库,在无互联网连接的 Windows Server 上扫描缺失的安全更新,自动导出补丁清单至桌面 CSV。
背景
在隔离网络(内网/离线服务器)环境中,Windows Server 无法直接连接 Microsoft Update 服务。本方案使用微软官方提供的 wsusscn2.cab 离线扫描数据库,在无互联网环境下完成安全补丁缺失检测。
前置准备
- 从联网机器下载 wsusscn2.cab 并拷贝至目标服务器
C:\wsusscn2.cab - 目标服务器需安装 Windows Update Agent (WUA)
- 以管理员权限运行 PowerShell
完整脚本
1. 初始化 COM 对象并校验 cab 文件
powershell
# 重置所有变量,清空失效COM会话
Remove-Variable -Name UpdateSession,UpdateServiceManager,UpdateService,UpdateSearcher -ErrorAction SilentlyContinue
$cabPath = "C:\wsusscn2.cab"
# 校验 cab 文件
if (-not (Test-Path $cabPath)) {
Write-Error "C:\wsusscn2.cab 文件不存在"
pause; exit 1
}2. 创建 UpdateSession 并注册离线扫描服务
powershell
try {
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
Write-Host "✅ COM UpdateSession 创建成功"
$UpdateServiceManager = New-Object -ComObject Microsoft.Update.ServiceManager
$UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", $cabPath)
Write-Host "✅ 离线扫描服务注册成功"
}3. 配置离线搜索器并执行扫描
powershell
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$UpdateSearcher.ServerSelection = 3 # ssOthers
$UpdateSearcher.ServiceID = [string]$UpdateService.ServiceID
Write-Host "✅ 离线搜索器配置完成,开始扫描(内存占用高,请等待)"
# 离线检索未安装的安全更新
$SearchResult = $UpdateSearcher.Search("IsInstalled=0 and Type='Software'")
$missingUpdates = $SearchResult.Updates4. 结果处理与 CSV 导出
powershell
if ($missingUpdates.Count -eq 0) {
Write-Host "`n🟢 扫描完成:本机无缺失安全更新" -ForegroundColor Green
exit 0
}
Write-Host "`n🟡 扫描完成,共发现 $($missingUpdates.Count) 个缺失更新:`n"
$outputList = @()
for ($i = 0; $i -lt $missingUpdates.Count; $i++) {
$u = $missingUpdates.Item($i)
$kbList = $u.KBArticleIDs -join ",KB"
$kbText = if ($kbList) { "KB$kbList" } else { "无KB编号" }
$info = [PSCustomObject]@{
序号 = $i + 1
KB编号 = $kbText
更新标题 = $u.Title
发布日期 = $u.ReleaseDate
是否关键更新 = $u.IsCriticalUpdate
}
$outputList += $info
Write-Host "$($info.序号). [$($info.KB编号)] $($info.更新标题)"
}
# 导出桌面 CSV 清单
$csvPath = "$env:USERPROFILE\Desktop\离线缺失补丁清单.csv"
$outputList | Export-Csv $csvPath -NoTypeInformation -Encoding UTF8
Write-Host "`n🟢 清单已导出至桌面:$csvPath"5. 异常处理
powershell
catch {
Write-Error "❌ 扫描异常:$_"
Write-Host "排查方向:
1. wsusscn2.cab签名损坏 → 重新下载官方cab覆盖
2. 内存不足 → 关闭所有后台软件再执行
3. WUA组件损坏 → 执行修复命令重启电脑"
}输出示例
| 序号 | KB编号 | 更新标题 | 是否关键 |
|---|---|---|---|
| 1 | KB5034765 | 2024-02 Cumulative Update for Windows Server 2022 | True |
| 2 | KB5034129 | Security Update for Windows Server 2022 | False |
常见问题
Q: 扫描过程中内存占用很高怎么办?
wsusscn2.cab 解压后约 1.2GB,扫描时 WUA 会将其加载到内存。建议在扫描前关闭其他后台程序。
Q: cab 文件签名损坏?
重新从微软官方下载 wsusscn2.cab,确保下载完整(约 400MB)。