重复输出 Windows 更新信息

重复输出 Windows 更新信息

该脚本生成了我在 Windows 中所做的更新的信息。Get-WuaHistory执行最后一个命令时,记录出现重复。

我没有成功使用帖子中的信息:
如何选择独特的

我尝试改变命令:
Select-Object -First 1 -ExpandProperty Name

到:
Select-Object -Unique -Property Name

但记录总是显示为重复。

脚本

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,50) | 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 get-unique
    $_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.RevisionNumber -Name RevisionNumber get-unique
    $_ | Add-Member -MemberType NoteProperty -Value $Product -Name Product -PassThru get-unique
    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-WuaHistory | Format-Table | More

通过更改以下内容,我能够生成不重复的报告:

$_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.UpdateId -Name UpdateId

至 – 在每个 Add-Member 行中包含 get-unique 命令:

$_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.UpdateId -Name UpdateId get-unique

然而,在报告生成之前,我收到以下信息消息:

Add-Member : The SecondValue parameter is not required for a member of type "NoteProperty" and must not be specified. Do not specify the SecondValue parameter when adding members of this
type.
No C:\Users\CMG\Desktop\Check_updates.ps1:30 caractere:10
+     $_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.UpdateId ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Add-Member], InvalidOperationException
    + FullyQualifiedErrorId : Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand

如何生成没有重复记录的信息?

答案1

我创建了$UpdId变量,并且在创建$history = $session.QueryHistory("",0,50)变量的过程中包含了 Where-object 命令来仅选择要由 ForEach-Object 处理的非重复对象:

有效:

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
}
$UpdId = ""
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,50) | ? { if ("$UpdId" -ne "$_.UpdateIdentity.UpdateId") { 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 $_ 
    $UpdId = $_.UpdateIdentity.UpdateId    
   }                                                      
  }
 }
  #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-WuaHistory | Format-Table | More

相关内容