locked
Windows Update reporting RRS feed

  • Question

  • Good day,

    I have a script I found that I would like to use that creates a CSV file for Windows update status of PC's in a txt file. The script below works exactly how I want the report to show, but I don't want to have to run it manually on each PC. I was playing with using a 'foreach' statement that would call out each PC in the list shown at the end. Any help is appreciated for this PS Noob. 

    #Script name WuaHistory.ps1
    
    #https://www.thewindowsclub.com/check-windows-update-history-using-powershell
    #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,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
    $_ | 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 | Export-Csv -Path .\WuaData.csv -NoTypeInformation

    I was playing with the idea below and using the 'foreach' statement, but am unable to get it to work.

    #Invoke Credentials
    $cred = get-credential 
    
    #Pull list of computers
    $Computers = get-content -Path ".\list.txt" 
    
    ForEach ($Computer in $Computers)
    
    Invoke-Command -ComputerName $Computer -filepath ".\WuaHistory.ps1" -credential $cred 

    Thank you

    Christopher


    • Edited by Christopher Irish Tuesday, January 22, 2019 2:48 PM formatting
    • Moved by Bill_Stewart Friday, March 15, 2019 6:18 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, January 21, 2019 10:18 PM

All replies

  • Please post code using the code posting tool provided on the toolbar.  You post is unreadable and cannot be copied correctly.  To get help from others it is best if you make the issue as easy to understand as possible.  Good code posting practices are required by his site,


    \_(ツ)_/

    Monday, January 21, 2019 10:27 PM
  • You can download the WU module from the PowerShellGallery and it will have to code needed to do what you ask.

    Install-Module  PSWindowsUpdate


    \_(ツ)_/

    Monday, January 21, 2019 10:30 PM
  • Also if you are on Windows 10 the module and commands are already provided:

    Get-Command -Module WindowsUpdateProvider

    The commands that can be remoted accept a computer name.  Most WU commands cannot be remoted even with Invoke.


    \_(ツ)_/

    Monday, January 21, 2019 10:37 PM