Skip to content

Instantly share code, notes, and snippets.

@Geogboe
Created January 12, 2018 17:26
Show Gist options
  • Save Geogboe/ea26118c526342127dc5ce3313c10726 to your computer and use it in GitHub Desktop.
Save Geogboe/ea26118c526342127dc5ce3313c10726 to your computer and use it in GitHub Desktop.
PowerShell Function: Returns all download links for a given patch provided by GUID
function Get-PatchDownloadURLs {
<#
.SYNOPSIS
Returns all download links for a given patch
.DESCRIPTION
A PATCH is not just a KB article ID, at least not in this sense. A patch
is a GUID that represents a KB applied to a **specific** computer.
.NOTES
Why am I doing it like this?
Because when you parse the Microsoft Update catlog for a given KB article
your going to find tons of results for tons of different products. Microsoft
also doesn't make it easy to select a different product because not only are all
products NOT listed in the 'products' column, but the product names don't match the
real product names. The way around this is to use a function like: Get-KBArticle ( different repo )
to return all GUIDs for a given KB, then filter down to the products you want,
then search for the download links for the GUIDS representing those patches
#>
[CmdletBinding()]
param (
# GUID - this is the unique identify for this patch
# If you search the Microsoft update catalog and click on a particular results
# the guid will appear in the address bar for the corresponding update
# that pops up
[Parameter(Mandatory)]
[string]
$GUID
)
begin {
# Stop on all errors
$ErrorActionPreference = "Stop"
Write-Verbose (
"Searching for download links for patch with guid: {0}" -f
$GUID
)
}
process {
# Array to store all links
$DownloadUrls = @()
# This code modified and based on http://stealthpuppy.com/powershell-download-import-updates-mdt/
# Only way to find download links is to launch the download page
# for a given KB article
# To do that, we need to build a form and submit it as this page is done via js on the website
# I have NOT idea how stealthpuppy found out about this hidden backend form!
$ProgressPreference = "SilentlyContinue" # Speeds up invoke webrequest
# Build body
$Post = @{
size = 0;
updateID = $GUID;
uidInfo = $GUID
} | ConvertTo-Json -Compress
$PostBody = @{
updateIDs = "[$Post]"
}
$Response = Invoke-WebRequest `
-Uri 'http://www.catalog.update.microsoft.com/DownloadDialog.aspx' `
-UseBasicParsing `
-Method POST `
-Body $PostBody
# Parse out download links
$DownloadUrls += $Response |
Select-Object -ExpandProperty Content |
Select-String -AllMatches -Pattern "(http[s]?\://download\.windowsupdate\.com\/[^\'\""]*)" |
ForEach-Object { $_.Matches.Value }
$ProgressPreference = "Continue"
return $DownloadUrls
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment