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

Blog


    September 29

    Powershell and the Windows Update API

     
    Microsoft published example code to use Windows Scripting Host in order to apply updates leveraging the Windows Update API. Having nothing better to do, I decided to port this code over to Powershell and pretty it up a bit with Winforms. This works with Windows Update, but it definitely works better with a WSUS server and clients configured to use that server.
     
    What's Required:
    Windows XP or higher
    .NET Framework 2.0 or higher
    Powershell v1.0
    A directory on your %SystemDrive% called, "Scripts"
    A JPG file in the "Scripts" directory called, "Example.JPG"
     
    What to be aware of:
    After applying updates, the computer will be restarted if any of the patches require a reboot. Watch out for the Internet Explorer 7 package. You might want to have Internet Explorer 7 installed prior to running this program, even though it is not required.
     
    What to do:
    Paste the following code into a Powershell window, or save it as a Powershell file and execute it.
     
    ###################################################################################################################################### # Windows Update through Powershell v1.0
    ######################################################################################################################################
    $UpdateSession = New-Object -com Microsoft.Update.Session
    $UpdateSearcher = $UpdateSession.CreateupdateSearcher()
    $SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")
    $UpdateLowNumber = 0
    $UpdateHighNumber = 1
    $NumberofUpdates = $searchResult.Updates.Count
    [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    $Form2 = New-Object Windows.Forms.Form
    $Form2.ControlBox = 0
    $Form2.MinimizeBox = 0
    $Form2.MaximizeBox = 0
    $Form2.ShowInTaskbar = 0
    $Form2.FormBorderStyle = "FixedToolWindow"
    $Form2.ShowIcon = 0
    $Form2.Width = 1024
    $Form2.Height = 768
    $Form2.TopLevel = 1
    $Form2.TopMost = 1
    $Form2.StartPosition = "Manual"
    $Form2.Location = "1,1"
    $Form2.Opacity = 1.00
    $Form2.Visible = 0
    $Form2.Enabled = 0
    $PercentDisplay1 = New-Object System.Windows.Forms.RichTextBox
    $PercentDisplay1.Location = "20,705"
    $PercentDisplay1.Width = 115
    $PercentDisplay1.Height = 20
    $PatchDisplay1 = New-Object System.Windows.Forms.RichTextBox
    $PatchDisplay1.Location = "145,705"
    $PatchDisplay1.Width = 855
    $PatchDisplay1.Height = 20
    $PictureBox1 = New-Object Windows.Forms.PictureBox
    $PictureBox1.Width = 1024
    $PictureBox1.Height = 768
    $PictureBox1.Image = [System.Drawing.Image]::FromFile("$env:SystemDrive\Scripts\Example.JPG")
    $PictureBox1.SizeMode = "StretchImage"
    $PictureBox1.BorderStyle = "None"
    $ProgressBar2 = New-Object Windows.Forms.ProgressBar
    $ProgressBar2.Location = "20,735"
    $ProgressBar2.Height = 20
    $ProgressBar2.Width = 980
    $ProgressBar2.Minimum = 1
    $ProgressBar2.Maximum = $NumberofUpdates
    $ProgressBar2.Style = 1
    $ProgressBar2.ImeMode = 1
    $Form2.Controls.Add($ProgressBar2)
    $Form2.Controls.Add($PercentDisplay1)
    $Form2.Controls.Add($PatchDisplay1)
    $Form2.Controls.Add($PictureBox1)
    while ($UpdateHighNumber -le $NumberofUpdates) {
    $UpdatesToDownload = New-Object -com Microsoft.Update.UpdateColl
    $Update = $searchResult.Updates.Item($UpdateLowNumber)
    if ($Update.EulaAccepted -eq 0) {$Update.AcceptEula()}
    $UpdatesToDownload.Add($Update)
    $Downloader = $UpdateSession.CreateUpdateDownloader()
    $Downloader.Updates = $UpdatesToDownload
    $Downloader.Download()
    $UpdatesToInstall = New-Object -com Microsoft.Update.UpdateColl
    $UpdatesToInstall.Add($Update)
    $ProgressBarPercent = 100 / ($ProgressBar2.Maximum / $ProgressBar2.Value)
    $ProgressBarConvertedPercent = [INT]$ProgressBarPercent
    $PercentDisplay1.Text = "$ProgressBarConvertedPercent% Complete"
    $PatchDisplay1.Text = $update.Title
    $Form2.Show()
    $Form2.Refresh()
    $Form2.BringToFront()
    $Installer = $UpdateSession.CreateUpdateInstaller()
    $Installer.Updates = $UpdatesToInstall
    $InstallationResult = $Installer.Install()
    $InstallationResult.ResultCode
    $InstallationResult.RebootRequired
    $UpdateLowNumber = $UpdateLowNumber + 1
    $UpdateHighNumber = $UpdateHighNumber + 1
    if ($ProgressBar2.Value -lt $ProgressBar2.Maximum) {$ProgressBar2.Value = $ProgressBar2.Value + 1}
    }
    $Form2.Close()
    $ComputerStatus = New-Object -com Microsoft.Update.SystemInfo
    if ($ComputerStatus.RebootRequired -eq 1) {cmd /c $env:WinDir\System32\Shutdown.exe -r -f -t 10 -c "Patching Complete."}