Windows Update Module - Get-wulist script problem

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Doug Morrison Doug Morrison 10 months, 1 week ago.

  • Author
    Posts
  • #34458
    Profile photo of Doug Morrison
    Doug Morrison
    Participant

    Hi All,

    I have been playing with the PSWindowsUpdate module and have come out with the below script. It works well and sends me an emails with the an attached copy of updates available on each server. However, it seems that if one of the servers doesn't require any updates, the HTML report comes back blank. Does anyone have any ideas on this?

    ` $Subject = "Server Updates as of $(Get-Date)"
    $Attachment = "C:\Powershell\ServerUpdates\AvailableUpdates.htm"
    $Body = "Please see attached the latest Server updates"

    # Measure how long it takes to run the script
    $StartDTM = (get-date)

    # Import the Update Module
    ipmo '\\ServerShare\SCRIPTS$\PSWindowsUpdate'

    $Servers = (Get-Content "C:\Powershell\ServerUpdates\test.txt")

    #embed a stylesheet in the html header
    $head = @"

    body { background-color:#dddddd;
    font-family:Tahoma;
    font-size:12pt; }
    td, th { border:1px solid black;
    border-collapse:collapse; }
    th { color:white;
    background-color:black; }
    table, tr, td, th { padding: 2px; margin: 0px }
    table { margin-left:50px; }

    "@

    $Fragment = " Required Server Updates $(Get-Date) "

    $Updates = Get-WUList -ComputerName $Servers

    # group data by KB Number
    $Groups=$Updates | Group-Object -Property KB

    # Check Updates for each server
    ForEach ($Update in $Groups) {
    $Fragment+="$($Update.name)"

    # define a collection of Updates from the group object
    $KB=$Update.group

    # create an html fragment
    $Fragment += $KB | Select-Object -Property ComputerName,KB,Status,Title,Size | ConvertTo-HTML -Fragment -As Table | Out-String

    }
    $EndDTM = (Get-Date)

    $Footer=("Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds")
    $Fragment+=$Footer

    ConvertTo-Html -head $head -body $Fragment | Out-File C:\Powershell\ServerUpdates\AvailableUpdates.htm

    Send-MailMessage -SmtpServer ServerFQDN -From service@domain.com -Subject $Subject -To user@domain.com -Attachments $Attachment -Body $Body -BodyAsHtml
    `

  • #34488
    Profile photo of Brian B
    Brian B
    Participant

    I would say you'd want to look at this line specifically:

    $Updates = Get-WUList -ComputerName $Servers
    

    When one of the servers has no updates, if you were to set your variable for $servers and then just run the above line to Get-WUList from $servers, does $Updates end up having any data in it or does it receive an error or end up otherwise blank?

    Brian

  • #34530
    Profile photo of Doug Morrison
    Doug Morrison
    Participant

    Hi Brian,

    Thanks for the reply. No error message is given. The report generated has the title, but no contents, regardless of there being subsequent servers queried that are known to require updates.

  • #34554
    Profile photo of Doug Morrison
    Doug Morrison
    Participant

    OK, I think I have gotten around this. Thanks Brian for putting me on the right track.

    I have added an IF statement within the loop that will check if the $Update variable is null. If it is, it will append the report with a "Server does not require updates" statement.

    Cheers!

    Doug

You must be logged in to reply to this topic.