James's profile"Funner" is not a word.BlogListsNetwork Tools Help

Blog


    April 22

    WSUS 3.0 and Powershell

    I installed WSUS 3.0 a few months back and given the size of the environment and the hardware I had at my disposal, it made perfect sense at that time to implement WSUS 3.0 on Windows Server 2003, Standard Edition (64-bit).

    While the installation went without a hitch and clients reported in as I would expect, I turned to the WSUS API Samples to further automate a few tasks. To my dismay, I discovered that these binaries were written in 32-bit and could not properly query a 64-bit implementation of WSUS 3.0.

    A little research lead me to the WSUS Team Blog where I was searching for a roadmap on 64-bit versions of the WSUS API Samples, when I uncovered some sample scripts for Powershell written by the WSUS team. Sweet!

    With a little modification and testing of the samples, I have managed to build something that suits my needs from WSUS in terms of reports. These scripts also work in 32-bit versions of WSUS 3.0 and with WSUS 3.01 SP1.

    What's Required:
    WSUS 3.0 or higher 32-bit or 64-bit
    .NET Framework 2.0 or higher
    Powershell v1.0
    A directory on your %SystemDrive% called, "Scripts"

    What to be aware of:
    Modify the following values as needed
    $SmtpClient.Host = "YOURMAILSERVER.YOURDOMAIN.COM"
    $MailMessage.from = "YOU@YOURDOMAIN.COM"
    $MailMessage.To.add("SOMEONE@YOURDOMAIN.COM,SOMEONE@YOURDOMAIN.COM")
    Uncomment out and modify the following variable if needed
    # $MailMessage.Bcc.add(SOMEONE@YOURDOMAIN.COM)

    What to do:
    Paste the following code into a Powershell window, or save it as a Powershell file and execute it. This script will email the information as a 'csv' file attachment.

    ######################################################################################################################################
    # Windows Update through Powershell v1.0
    ######################################################################################################################################

    $ReportDate = Get-Date

    Set-Content $ENV:SystemDrive\Scripts\MissingUpdatesBrief.csv """Server"",""Patches Needed"""

    [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null

    if (!$wsus) {
            $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
    }

    $ComputerScope = new-object Microsoft.UpdateServices.Administration.ComputerTargetScope;
    $ComputerScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::NotInstalled;

    $UpdateScope = new-object Microsoft.UpdateServices.Administration.UpdateScope;
    $UpdateScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::NotInstalled;

    $Computers = $wsus.GetComputerTargets($ComputerScope);

    $Computers | ForEach-Object {
    $WsusComputerName = $_.FullDomainName
    $UpdatesForReboot = $_.GetUpdateInstallationInfoPerUpdate($UpdateScope);
    $UpdatesForReboot | foreach-object {
    $NeededUpdate = $wsus.GetUpdate($_.UpdateId);
    $WsusUpdateCount = $UpdatesForReboot.Count
    }
    Write-Output """$WsusComputerName"",""$WsusUpdateCount""" | Out-File $ENV:SystemDrive\Scripts\MissingUpdatesBrief.csv -encoding ASCII -append -width 512

    }

    $ReportSorted = import-csv $ENV:SystemDrive\Scripts\MissingUpdatesBrief.csv | Sort Server
    $ReportSorted | export-csv $ENV:SystemDrive\Scripts\MissingUpdatesBrief.csv -notype -force

    $SmtpClient = new-object system.net.mail.smtpClient
    $MailMessage = New-Object system.net.mail.MailMessage
    $SmtpClient.Host = "YOURMAILSERVER.YOURDOMAIN.COM"
    $MailMessage.from = "YOU@YOURDOMAIN.COM"
    $MailMessage.To.add("SOMEONE@YOURDOMAIN.COM,SOMEONE@YOURDOMAIN.COM")
    # $MailMessage.Bcc.add("SOMEONE@YOURDOMAIN.COM")
    $MailMessage.Subject = "Computer Status Brief Report"

    $MailMessage.Body = "
    <html><body>
    <dl>
      <dd>
        <em>Please find the attached report detailing the status of all servers requiring updates.<br>
        <br>
            Servers that are up to date will not be listed on this report.
        <br>
        <br>
        <br>
        <br>
        <br>
            Report time: $ReportDate
        <em>
      </dd>
    </dl>
    </body></html>
    "

    $MailMessage.priority = "high"
    $MailMessage.IsBodyHtml = 1
    $MailMessage.Attachments.Add("$ENV:SystemDrive\Scripts\MissingUpdatesBrief.csv")
    $Smtpclient.Send($MailMessage)

    exit

     

    del.icio.us Tags: ,

    Technorati Tags: ,