Automate Downloading Patches in a Qualys Vulnerability Report

Adam Bertram

Adam Bertram

Read more posts by this author.

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.

$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

## 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 "$($_.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

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!