【问题标题】:How do I get the Update History from Windows Update in PowerShell?如何在 PowerShell 中从 Windows 更新获取更新历史记录?
【发布时间】:2017-05-28 07:47:46
【问题描述】:

谁能给我一个示例,说明如何在 PowerShell 中从 Windows 更新获取更新历史记录?

我在这里找到了 API 文档:https://msdn.microsoft.com/en-us/library/windows/desktop/bb394842(v=vs.85).aspx

但是,关于如何从 PowerShell 调用它的内容很少。

【问题讨论】:

    标签: powershell windows-update


    【解决方案1】:

    以下是如何从 PowerShell 查询 WUA 历史记录的示例。首先,我将定义几个函数来提供帮助。

    # 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
    }  
    

    定义完这些函数后,我们就可以得到更新了

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

    【讨论】:

    • 也删除排除后卫选项。我相信我们不需要单独使用它。 :)
    • 为了可读性我已经删除并重构了一些其他代码以提高可读性,但请尝试在机器上实际使用它,看看您是否同意是否不需要排除防御者选项。在 Windows 10 中,Windows 更新历史记录窗口本身不包括防御者。
    • 太棒了...是的,可以。
    • 显然技术上不需要,但排除防守者在 99% 的情况下确实有帮助。我将所有内容都包装在 foreach-object 中: if ($_.Title -notlike "Definition Update*") {
    • 第 4 版具有排除定义更新的方法,因此 WUA 不会进行评估它们的工作。我被要求从样本中删除它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2013-05-04
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多