我在使用 Windows Server 2012 上的命令时遇到了麻烦,该Get-HotFix
命令没有返回所有已安装的 KB,而 Windows Update 在查询时却显示服务器上实际安装了什么。
Get-HotFix 脚本:
Get-HotFix | Select-Object HotFixID | Where-Object { $_.HotFixId.ToUpper().StartsWith("KB") } | ForEach-Object { Write-Output "PACKAGE:$($_.HotFixId)" }
Windows 更新脚本
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$Searcher.ServerSelection = 2
$InstalledUpdates = $Searcher.Search("IsInstalled=1")
ForEach ($Update in $InstalledUpdates.Updates) {
Foreach ($KB_ID in $Update.KBArticleIDs) {
Write-Output "PACKAGE_WUA:KB$($KB_ID)|$($Update.Identity.UpdateID)"
}
}
Get-HotFix 脚本的输出:
PACKAGE:KB3191564
PACKAGE:KB2868626
PACKAGE:KB2883200
PACKAGE:KB2887595
PACKAGE:KB2903939
PACKAGE:KB2911106
PACKAGE:KB2919355
PACKAGE:KB2919394
PACKAGE:KB2920189
PACKAGE:KB2928680
PACKAGE:KB2934520
PACKAGE:KB2938066
PACKAGE:KB2954879
PACKAGE:KB2955164
PACKAGE:KB2961908
PACKAGE:KB2967917
PACKAGE:KB2975061
PACKAGE:KB2977765
PACKAGE:KB2978041
PACKAGE:KB2978126
PACKAGE:KB2989930
PACKAGE:KB2999226
PACKAGE:KB3000483
PACKAGE:KB3000850
PACKAGE:KB3003057
PACKAGE:KB3004365
PACKAGE:KB3004545
PACKAGE:KB3012702
PACKAGE:KB3013172
PACKAGE:KB3013769
PACKAGE:KB3013791
PACKAGE:KB3013816
PACKAGE:KB3014442
PACKAGE:KB3019978
PACKAGE:KB3021910
PACKAGE:KB3023222
PACKAGE:KB3023266
PACKAGE:KB3024751
PACKAGE:KB3024755
PACKAGE:KB3030947
PACKAGE:KB3033446
PACKAGE:KB3035126
PACKAGE:KB3036612
PACKAGE:KB3037579
PACKAGE:KB3038002
PACKAGE:KB3042085
PACKAGE:KB3044374
PACKAGE:KB3044673
PACKAGE:KB3045634
PACKAGE:KB3045685
PACKAGE:KB3045717
PACKAGE:KB3045719
PACKAGE:KB3045755
PACKAGE:KB3045999
PACKAGE:KB3046017
PACKAGE:KB3046737
PACKAGE:KB3054169
PACKAGE:KB3054203
PACKAGE:KB3054256
PACKAGE:KB3054464
PACKAGE:KB3055323
PACKAGE:KB3055343
PACKAGE:KB3055642
PACKAGE:KB3059317
PACKAGE:KB3060793
PACKAGE:KB3061512
PACKAGE:KB3063843
PACKAGE:KB3064209
PACKAGE:KB3065822
PACKAGE:KB3065988
PACKAGE:KB3068708
PACKAGE:KB3071756
PACKAGE:KB3074228
PACKAGE:KB3074548
PACKAGE:KB3075853
PACKAGE:KB3077715
PACKAGE:KB3078071
PACKAGE:KB3078405
PACKAGE:KB3078676
PACKAGE:KB3080149
PACKAGE:KB3082089
PACKAGE:KB3084135
PACKAGE:KB3084905
PACKAGE:KB3086255
PACKAGE:KB3087137
PACKAGE:KB3091297
PACKAGE:KB3094486
PACKAGE:KB3095701
PACKAGE:KB3097997
PACKAGE:KB3098779
PACKAGE:KB3099834
PACKAGE:KB3100473
PACKAGE:KB3102429
PACKAGE:KB3102467
PACKAGE:KB3103616
PACKAGE:KB3103696
PACKAGE:KB3103709
PACKAGE:KB3109103
PACKAGE:KB3109976
PACKAGE:KB3110329
PACKAGE:KB3112148
PACKAGE:KB3112336
PACKAGE:KB3115224
PACKAGE:KB3118401
PACKAGE:KB3121261
PACKAGE:KB3122654
PACKAGE:KB3123245
PACKAGE:KB3124275
Windows 更新脚本的输出:
PACKAGE_WUA:KB925673|07609d43-d518-4e77-856e-d1b316d1b8a8
PACKAGE_WUA:KB2538243|729a0dcb-df9e-4d02-b603-ed1aee074428
PACKAGE_WUA:KB3023266|8ffb7a9e-cafe-456f-a219-13b3e7a56ed2
PACKAGE_WUA:KB3019978|2628719c-4abd-46e2-93e8-fe53c19c3117
PACKAGE_WUA:KB3000483|4361108d-d09b-4c4d-8b2f-f1167f7b8c57
PACKAGE_WUA:KB3035126|3fea2186-fb39-41f1-9214-55a5bb5552e6
PACKAGE_WUA:KB2919355|8452bac0-bf53-4fbd-915d-499de08c338b
PACKAGE_WUA:KB3045755|36696f1a-eb7a-48db-a69b-fae68b1fbf58
PACKAGE_WUA:KB3045999|4678f8b9-0b81-4f8b-853f-4adfed9262a6
PACKAGE_WUA:KB3045685|5d50743b-e648-4acc-8986-a7bc5aa53f71
PACKAGE_WUA:KB3055642|02ee413c-7bbb-4fe0-bd97-9e931a2208e1
PACKAGE_WUA:KB3059317|8bb29af2-47b2-439e-b999-3176c23a8e8b
PACKAGE_WUA:KB3061512|54110383-c46c-4424-a8ef-9cd6b7a20025
PACKAGE_WUA:KB3004365|fb95c1c8-de23-4089-ae29-fd3351d55421
PACKAGE_WUA:KB3046017|4b7876f0-b962-4c4c-9e45-fe35b0c801ce
PACKAGE_WUA:KB3071756|f7c02df0-0cb8-4a56-b367-7efd5c9bc9ac
PACKAGE_WUA:KB3082089|7c5f1dad-2f35-4120-b4ad-c5947edf0567
PACKAGE_WUA:KB3084135|04f04253-f803-43b8-8b37-6841c614dd9f
PACKAGE_WUA:KB3086255|c848ed4a-5e2e-43dc-bd1b-cb2f717e90cd
PACKAGE_WUA:KB3109103|d928f50f-191e-4858-b546-acda983da182
PACKAGE_WUA:KB3110329|27ae312b-1b1a-4f18-a574-3dfe46244d3a
PACKAGE_WUA:KB3126593|3ce61d87-7153-4540-a74b-fd0065edfdde
PACKAGE_WUA:KB3133043|1bc49ccc-409b-4d07-93b7-cea850ea0d78
PACKAGE_WUA:KB3126041|aa793ff1-f4b7-42b1-9f24-6f11b50f952c
PACKAGE_WUA:KB3126434|6b6a5779-3fe5-4789-aba6-0c3e9b802b37
PACKAGE_WUA:KB3126587|214b1f40-24ed-4dae-9e76-237763c5c8ca
PACKAGE_WUA:KB3135998|8069b13a-3bf0-4e14-a80b-d5cd3acc42ed
PACKAGE_WUA:KB3139398|dd382658-effd-4e24-abca-877e1915bf6d
PACKAGE_WUA:KB3139914|06eae584-188d-4a35-805a-ef98beddcd3d
PACKAGE_WUA:KB3146723|209e2d1e-330f-4dcc-b705-5999c2f38385
PACKAGE_WUA:KB3156059|05ba4c3c-81c2-4c19-9899-a4a18716cddb
PACKAGE_WUA:KB3155784|20d1a457-eca7-488d-a870-96d7374cb436
PACKAGE_WUA:KB3159398|d9f4e1b5-4477-4d83-8c27-111ce0e88dd8
PACKAGE_WUA:KB3161949|67a00639-09a1-4c5f-83ff-394e7601fc03
PACKAGE_WUA:KB3162343|ba0f75ff-19c3-4cbd-a3f3-ef5b5c0f88bf
PACKAGE_WUA:KB3172729|cdde339c-ebdb-4a16-add4-fb196a5053a8
PACKAGE_WUA:KB3175024|77798aee-dc0e-409a-875c-7134c8a93766
PACKAGE_WUA:KB3178539|e90f9283-7304-4a98-a5ec-98bd2a3163e7
PACKAGE_WUA:KB3205400|ff4c348a-c6c2-4bbc-af33-01ef3ae1cc6e
PACKAGE_WUA:KB4012213|80bc2b42-a953-4096-8595-130e9a9c9fb9
PACKAGE_WUA:KB4015547|79629e4e-67eb-438d-9420-17c673012731
PACKAGE_WUA:KB4019213|2913676b-33a1-4557-9160-f36bfed5f294
PACKAGE_WUA:KB4019111|2e2dc8c8-c324-4968-9461-1c5c6f1190c1
PACKAGE_WUA:KB4022717|7b6c010d-36ea-4bb4-9d06-9e44c1235690
PACKAGE_WUA:KB4025333|a8091733-a526-4480-afe7-72a1b3385439
PACKAGE_WUA:KB4034672|1d84c38c-d2dd-4ad8-acd7-19d9391de17f
PACKAGE_WUA:KB4038793|9516efa8-6493-43de-979c-ebf2aa89aa69
PACKAGE_WUA:KB4041687|e36e24fc-789e-483f-bb72-01a3ec3eea74
PACKAGE_WUA:KB4048961|896142dc-9329-403d-98e0-229567e995d9
PACKAGE_WUA:KB4054522|89bb3c1a-4825-4ae3-a42f-ad0ae8c86c1a
PACKAGE_WUA:KB4056898|29159163-0da5-456d-92a5-9bd853a401ae
PACKAGE_WUA:KB4055271|0f6efea5-e7fe-4bab-80fa-4033e4a017ab
PACKAGE_WUA:KB4074597|f5df629a-edb7-49f1-946d-18e4f0a860f0
PACKAGE_WUA:KB4088879|913c1d08-eb63-4b74-aeff-a476d1fbaa2c
PACKAGE_WUA:KB4093115|33be9fcf-aef5-44fd-8164-e6b8baa88549
PACKAGE_WUA:KB4103715|196f3a64-83fb-4123-a71b-3e7aa0534b4a
PACKAGE_WUA:KB4099639|041c9d83-7a16-4e5d-8f2b-35d5cca0fc62
PACKAGE_WUA:KB4284878|45636cfd-ab6b-4db5-a310-712f3663eae6
PACKAGE_WUA:KB4338824|007ce1f3-3a12-40ef-8389-322068b780a7
PACKAGE_WUA:KB4340006|1d638098-af3d-4ca5-84e1-db9cb0e82ae4
PACKAGE_WUA:KB4339284|d91c89a6-0ef2-4b57-961d-f3f657de9d66
PACKAGE_WUA:KB4343888|ed4981c9-7aa7-4e1f-bdd5-b43d2afae17a
PACKAGE_WUA:KB4345681|c601cf7a-ba23-4d6d-bcdc-c61c258e7f1f
PACKAGE_WUA:KB4457143|b4e784a4-9907-4d25-90ef-858bcafcf450
PACKAGE_WUA:KB4462941|07cf0ca5-397c-46ee-9093-b1e5dbce59bb
PACKAGE_WUA:KB4462901|cecc9666-c679-4104-9ba0-bfe206d181a5
PACKAGE_WUA:KB4468323|03a8098b-7740-40da-9082-00ea285035be
PACKAGE_WUA:KB4471322|39654137-cb30-429e-b09a-2c39055716c1
PACKAGE_WUA:KB4471983|0fcc811c-48de-4c15-8240-5ccdb8171d8b
PACKAGE_WUA:KB4480964|45e80aae-4277-4c87-bd05-5cc8c6dd672e
PACKAGE_WUA:KB4487123|aa180d6a-6b08-4f8b-8e93-682de0657164
PACKAGE_WUA:KB4487080|e69d46be-4fe0-4d50-baf6-6091109d90ab
PACKAGE_WUA:KB4487028|0b256bc7-170d-4ee7-a993-8e9fac37a589
PACKAGE_WUA:KB4486459|adbc9c77-f9ba-4224-8b5c-4ae453898c2b
PACKAGE_WUA:KB4489883|dded804a-129f-4644-899c-e49ccd2bad2c
PACKAGE_WUA:KB4490128|c49cf4bb-325d-4752-ae32-33e053fabaf2
PACKAGE_WUA:KB890830|c991073f-ca3e-467d-b5a0-c8574b289b96
PACKAGE_WUA:KB4493435|94debd3d-b7b7-42e0-9645-11a4e8c83772
PACKAGE_WUA:KB4493467|8693f170-0aea-4539-bc72-feffa6e947ba
PACKAGE_WUA:KB4493446|a946a42f-5514-49bd-8a5c-42bf6354d8e4
如您所见,命令输出中缺少以下 KB(61)Get-HotFix
:
KB925673, KB2538243, KB3126593, KB3133043, KB3126041, KB3126434, KB3126587, KB3135998, KB3139398, KB3139914, KB3146723, KB3156059, KB3155784, KB3159398, KB3161949, KB3162343, KB3172729, KB3175024, KB3178539, KB3205400, KB4012213, KB4015547, KB4019213, KB4019111, KB4022717, KB4025333, KB4034672, KB4038793, KB4041687, KB4048961, KB4054522, KB4056898, KB4055271, KB4074597, KB4088879, KB4093115, KB4103715, KB4099639, KB4284878, KB4338824, KB4340006, KB4339284, KB4343888, KB4345681, KB4457143, KB4462941, KB4462901, KB4468323, KB4471322, KB4471983, KB4480964, KB4487123, KB4487080, KB4487028, KB4486459, KB4489883, KB4490128, KB890830, KB4493435, KB4493467, KB4493446
例如,最后一个(KB4493446)是2019年4月的累计。
我的问题是:
- 是什么原因导致它
Get-HotFix
返回的 KB 比查询 Windows 更新时少? - 检索所有已安装的 KB 的良好解决方法是什么无需连接互联网?
答案1
您是否尝试过使用部署映像服务和管理(DISM)?
https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/what-is-dism
您可以直接使用 dism:
dism /online /get-packages
/online
这并不意味着您需要互联网,而只是针对当前系统。
如果您更喜欢 powershell,您也可以使用 Powershell DISM 包装器:
Get-WindowsPackage -Online
https://docs.microsoft.com/fr-fr/previous-versions/windows/powershell-scripting/dn376488(v=wps.620)
答案2
您尝试过 WMI 方法吗?
Get-WmiObject -class Win32_QuickFixEngineering -namespace "root\cimv2" | Select-Object -Property HotFixID | Out-String
编辑
忽略该答案。我相信 Get-HotFix 无论如何都会使用 WMI。我发现此解决方案这对我来说产生了不同的结果:
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$UpdateHistory = $Searcher.QueryHistory(0, $historyCount)
$KBs = @()
foreach ($Update in $UpdateHistory) {
[regex]::match($Update.Title,'(KB[0-9]{6,7})').value | Where-Object {$_ -ne ""} | foreach {
$KB = New-Object -TypeName PSObject
$KB | Add-Member -MemberType NoteProperty -Name KB -Value $_
$KB | Add-Member -MemberType NoteProperty -Name Title -Value $Update.Title
$KB | Add-Member -MemberType NoteProperty -Name Description -Value $Update.Description
$KB | Add-Member -MemberType NoteProperty -Name Date -Value $Update.Date
$KBs += $KB
}
}
$KBs | Select KB,Title,Description,Date
答案3
基于的答案New-Object -ComObject "Microsoft.Update.Session"
将不会显示在重置 Windows 更新(通过删除 SoftwareDistribution 文件夹完成)之前安装的任何更新,"Microsoft.Update.Session"
只会显示自 WU 重置以来安装的更新(WU 重置描述如下:https://stackoverflow.com/questions/45934703/reset-windows-updates)
Get-WindowsPackage -Online
无论如何都可以工作,因此它更可靠,但是它不会显示其他 MS 产品的更新(例如 SQL 服务器):
已安装软件与 Get-WindowsPackage 的比较
其他 MS 产品的更新显示在注册表中Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
,但常规 Windows 更新未列在那里,因此到目前为止我发现的唯一可靠方法是将注册表和Get-WindowsPackage -Online
EDIT
Get-WindowsPackage
还缺少一些 Windows 更新包,比较Get-WMIObject -Class Win32_QuickFixEngineering
(右侧)与左侧:Get-WindowsPackage
这里因此似乎唯一可靠的解决方案是结合多种数据源。