Here's a very convenient script that save you a ton of time although it'll only apply to a fairly small niche. My client has a Qualys vulnerability scanner that they use periodically to scan for security issues, missing patches, etc. This device scans the device and then produces a report of the actions you need to take to fix the vulnerabilities it found. A lot of the entries are for missing updates.

The report conveniently gives a Microsoft link to download the required patches. However, we were finding that on some of the servers there were dozens of patches to download and apply. Here's my first attempt at automating this process. This script takes an export of the Qualys report in MHT format. It then searches the report for any link that matches the OS of the server. If it finds one, it then follows the link, downloads the patch and places it into a directory.

param($scan_results_file,$OperatingSystem)
$download_path = 'C:\DOWNLOADPATH'

## Instantiate the IE COM object to read the MHT file
$ie = New-Object -ComObject 'InternetExplorer.Application'
## Read the MHT file with IE
$ie.Navigate2("file://$scan_results_file")

## Find all links that have your operating system in the name
$links = $ie.Document.getElementsByTagName('a') | ? {($_.innertext -like "*$OperatingSystem*") -and ($_.href -like 'http://*')} | select -ExpandProperty href
foreach ($link in $links) {
    ## Request the URL of the link found
    $r = Invoke-WebRequest -Uri $link -UseBasicParsing
    ## Find the download link in the requested URL
    $r.Links | ? {$_.outerhtml -like '*Download*'} | % {
        $y = Invoke-WebRequest "http://www.microsoft.com$($_.href)" -UseBasicParsing
    }
    $webclient = New-Object System.Net.WebClient
    ## Find the link in the download URL called Click Here and download and save the file
    $y.Links | ? {$_.outerhtml -like '*Click Here*'} | select -first 1 | % {$webclient.DownloadFile($_.href,"$download_path$(split-path $_.href -Leaf)")}
}

View this script on the Technet Script Center

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!