在 PowerShell 中收集完整的历史 Windows 更新历史记录

sea*_*ull 5 windows-update powershell

我试图使用 PowerShell 根据设置应用程序获取系统曾经安装的所有更新的列表,最终目标是两者之间进行 1:1 比较。
懒惰的技术博客将为您提供以下其中一项,它会轮询 Win32_QuickFixEngineering WMI 位置:

get-hotfix
wmic qfe list full
get-wmiobject win32_quickfixengineering
Run Code Online (Sandbox Code Playgroud)

但这并不能说明一切。根据微软的说法:

从 Windows Vista 开始,此类仅返回 Microsoft Windows Installer (MSI)、Windows Update、Microsoft Update 或 Windows Server Update Services 提供的更新。它不包括由基于组件的服务 (CBS) 或其他非修补程序或应用程序提供的更新。

为了补充这一点,您经常会看到与 PowerShell 中的“Windows.Update.Session”COM 对象交互的代码块执行类似的操作以获得已安装更新的(不同)列表:

$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
Run Code Online (Sandbox Code Playgroud)

但即使这样也无法获取完整的历史列表——我想是因为从中提取的列表是对作为累积安装的更新进行重复删除,并省略了为早期版本的 Windows 安装的更新。然而, “设置”
应用程序列出了一年多前在我的设备上安装的内容:

2020 年安装的更新列表

有没有办法使用 PowerShell 访问列表?设置从哪里获取它?

Cpt*_*ale 2

使用PSWindowsUpdate模块。该Get-WUHistorycmdlet 使用与您在问题中尝试过的相同的 windows update api (wua),但在后台执行了一些额外的操作以使内容可读:

$history = Get-WUHistory -Last 1000

# Larger than in Settings app, since it includes failed updates
$history.Count
62 

$history | sort date -desc | 
  Format-Table Date,KB,@{l='Category';e={[string]$_.Categories[0].Name}},Title
Run Code Online (Sandbox Code Playgroud)
Date                   KB        Category                           Title               
----                   --        --------                           -----               
7/16/2021 4:19:43 PM   KB5004237 Windows 10, version 1903 and later 2021-07 Cumulativ...
7/16/2021 4:00:44 PM   KB5003537 Windows 10, version 1903 and later 2021-07 Cumulativ...
7/15/2021 5:06:18 PM   KB890830  Windows 10                         Windows Malicious...
7/7/2021 4:22:58 PM    KB5004945 Windows 10, version 1903 and later 2021-07 Cumulativ...
Run Code Online (Sandbox Code Playgroud)

这是我认为最好的选择,尽管我注意到它似乎无法获取新更新类型(例如“通过启用包进行功能更新”)的 KB 编号,并且当前此模块的错误需要一个标志-Last (days)或它会永远循环。

我无法确认历史限制是多少,但我的历史限制确实比一年稍早一些。