简体   繁体   English

如何从PowerShell中的Windows Update获取更新历史记录?

[英]How do I get the Update History from Windows Update in PowerShell?

Can someone give me an example of how to get the Update History from Windows Update in PowerShell? 有人可以给我一个示例,说明如何从PowerShell中的Windows Update获取更新历史记录吗?

I found the API documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb394842(v=vs.85).aspx 我在这里找到API文档: https : //msdn.microsoft.com/zh-cn/library/windows/desktop/bb394842(v=vs.85).aspx

But, there is very little on how to call it from PowerShell. 但是,关于如何从PowerShell调用它的知识很少。

Here is a sample of how to query the WUA history from PowerShell. 这是有关如何从PowerShell查询WUA历史记录的示例。 First, I'll define a couple of functions to help. 首先,我将定义几个函数来提供帮助。

# Convert Wua History ResultCode to a Name
# 0, and 5 are not used for history
# See https://msdn.microsoft.com/en-us/library/windows/desktop/aa387095(v=vs.85).aspx
function Convert-WuaResultCodeToName
{
    param(
        [Parameter(Mandatory=$true)]
        [int] $ResultCode
    )

    $Result = $ResultCode
    switch($ResultCode)
    {
      2 {
        $Result = "Succeeded"
      }
      3 {
        $Result = "Succeeded With Errors"
      }
      4 {
        $Result = "Failed"
      }
    }

    return $Result
}

function Get-WuaHistory
{

  # Get a WUA Session
  $session = (New-Object -ComObject 'Microsoft.Update.Session')

  # Query the latest 1000 History starting with the first recordp     
  $history = $session.QueryHistory("",0,1000) | ForEach-Object {
     $Result = Convert-WuaResultCodeToName -ResultCode $_.ResultCode

     # Make the properties hidden in com properties visible.
     $_ | Add-Member -MemberType NoteProperty -Value $Result -Name Result
     $Product = $_.Categories | Where-Object {$_.Type -eq 'Product'} | Select-Object -First 1 -ExpandProperty Name
     $_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.UpdateId -Name UpdateId
     $_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.RevisionNumber -Name RevisionNumber
     $_ | Add-Member -MemberType NoteProperty -Value $Product -Name Product -PassThru

     Write-Output $_
  } 

  #Remove null records and only return the fields we want
  $history | 
      Where-Object {![String]::IsNullOrWhiteSpace($_.title)} | 
          Select-Object Result, Date, Title, SupportUrl, Product, UpdateId, RevisionNumber
}  

After these functions are defined, we can get the updates 定义这些功能后,我们可以获得更新

# Get all the update History, formatted as a table
Get-WuaHistory | Format-Table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Windows Update设置为从不使用PowerShell检查更新? - How do I set Windows Update to never check for updates with PowerShell? 我如何获得Powershell来检查计算机的Windows更新是否已过期30天以上 - How do i get powershell to check if my computer's Windows update are more than 30 days out of date 如何使用 PowerShell 更新 BuildNumber - How do I update BuildNumber using PowerShell 如何在Powershell中的变量中更新变量? - How do I update a variable in a variable in powershell? 如何从注册表更新Windows PowerShell会话环境变量? - How to update Windows PowerShell session environment variables from registry? Powershell,如何获取上次 Windows 更新安装的日期或至少检查更新的日期? - Powershell, How to get date of last Windows update install or at least checked for an update? 如何通过 Azure DevOps 发布管道中的 Azure Powershell 更新暂存槽的 IP 白名单? - How do I update the IP whitelist for a staging slot via Azure Powershell from an Azure DevOps Release Pipeline? Powershell 重置 Windows 更新 - Powershell Reset Windows Update 如何通过Powershell更新AppDynamics .net代理? - How do I update AppDynamics .net agent via Powershell? 如何使用PowerShell更新JSON文件(修补方法) - How do I update JSON file using PowerShell (Patch Method)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM