首页 > 电脑专区 > windows教程 >

如何使用PowerShell查看Windows Update更新历史记录

windows教程 2021-06-25 21:39:22

用户除了可以通过「Windows 设置」——「更新和安全」界面中查看更新历史记录外,还可以通过 PowerShell 来查看 Windows Update 更新历史记录。下面我们就介绍如何使用任务自动化和配置管理工具(如 PowerShell)列出 Windows Update 事件的所有历史记录。

此前我们也介绍过如何使用 PowerShell 查看 Windows 10 Build 升级历史记录

使用PowerShell查看Windows Update更新历史记录

1使用 Windows + X 快捷键打开快捷菜单——选择 Windows PowerShell(管理员)

2执行如下命令,即可查看到当前计算机中已安装补丁的详细信息,包括:标题、描述、KB 号、安装日期和状态等等。

wmic qfe list

如何使用PowerShell查看Windows Update更新历史记录

3也可以键入以下命令以列出修补程序 KB 及其关联的描述。

get-wmiobject -class win32_quickfixengineering

如何使用PowerShell查看Windows Update更新历史记录

当然,也可以向 Windows PC 更新历史记录写入查询函数,以返回指向 Windows 系统上匹配列表记录的指针。

  1. function Convert-WuaResultCodeToName
  2. {
  3. param(
  4. [Parameter(Mandatory=$true)]
  5. [int] $ResultCode
  6. )
  7.  
  8. $Result = $ResultCode
  9. switch($ResultCode)
  10. {
  11. 2 {
  12. $Result = "成功"
  13. }
  14. 3 {
  15. $Result = "成功但包含错误"
  16. }
  17. 4 {
  18. $Result = "失败"
  19. }
  20. }
  21.  
  22. return $Result
  23. }
  24.  
  25. function Get-WuaHistory
  26. {
  27.  
  28. $session = (New-Object -ComObject 'Microsoft.Update.Session')
  29.  
  30. $history = $session.QueryHistory("",0,1000) | ForEach-Object {
  31. $Result = Convert-WuaResultCodeToName -ResultCode $_.ResultCode
  32.  
  33. $_ | Add-Member -MemberType NoteProperty -Value $Result -Name Result
  34. $Product = $_.Categories | Where-Object {$_.Type -eq 'Product'} | Select-Object -First 1 -ExpandProperty Name
  35. $_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.UpdateId -Name UpdateId
  36. $_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.RevisionNumber -Name RevisionNumber
  37. $_ | Add-Member -MemberType NoteProperty -Value $Product -Name Product -PassThru
  38.  
  39. Write-Output $_
  40. }
  41.  
  42. $history |
  43. Where-Object {![String]::IsNullOrWhiteSpace($_.title)} |
  44. Select-Object Result, Date, Title, SupportUrl, Product, UpdateId, RevisionNumber
  45. }

在定义好函数之后,就可以用来获取更新:

Get-WuaHistory | Format-Table

如何使用PowerShell查看Windows Update更新历史记录



标签: 如何 使用 PowerShell 查看 Windows Update 更新

office教学网 Copyright © 2016-2021 office.mshxw.com. Some Rights Reserved. 备案号:晋ICP备2021003244-6号