Skip to content

Instantly share code, notes, and snippets.

@iainbrighton
Created July 13, 2017 08:40
Show Gist options
  • Save iainbrighton/243c9f89a80c5f294eb2e3f4ea0e11a3 to your computer and use it in GitHub Desktop.
Save iainbrighton/243c9f89a80c5f294eb2e3f4ea0e11a3 to your computer and use it in GitHub Desktop.
Finds the latest Windows 10/Server 2016 cumulative update(s)
function Find-CumulativeWindowsUpdate {
<#
.SYNOPSIS
Finds the latest Windows 10/Server 2016 cumulative update(s).
.EXAMPLE
Find-CumulativeWindowsUpdate
KB Description Uri
-- ----------- ---
KB4025338 2017-07 Cumulative Update for Windows 10 Version 1507 for x86-based Systems (KB... http://download.w...
KB4025338 2017-07 Cumulative Update for Windows 10 Version 1507 for x64-based Systems (KB... http://download.w...
KB4025344 2017-07 Cumulative Update for Windows 10 Version 1511 for x64-based Systems (KB... http://download.w...
KB4025344 2017-07 Cumulative Update for Windows 10 Version 1511 for x86-based Systems (KB... http://download.w...
KB4025339 2017-07 Cumulative Update for Windows Server 2016 for x64-based Systems (KB4025... http://download.w...
KB4025339 2017-07 Cumulative Update for Windows 10 Version 1607 for x64-based Systems (KB... http://download.w...
KB4025339 2017-07 Cumulative Update for Windows 10 Version 1607 for x86-based Systems (KB... http://download.w...
KB4025342 2017-07 Cumulative Update for Windows 10 Version 1703 for x64-based Systems (KB... http://download.w...
KB4025342 2017-07 Cumulative Update for Windows 10 Version 1703 for x86-based Systems (KB... http://download.w...
This command finds the latest Windows 10/Server 2016 cumulative updates for all build numbers and architectures.
.EXAMPLE
Find-CumulativeWindowsUpdate -Architecture x86
KB Description Uri
-- ----------- ---
KB4025338 2017-07 Cumulative Update for Windows 10 Version 1507 for x86-based Systems (KB... http://download.w...
KB4025344 2017-07 Cumulative Update for Windows 10 Version 1511 for x86-based Systems (KB... http://download.w...
KB4025339 2017-07 Cumulative Update for Windows 10 Version 1607 for x86-based Systems (KB... http://download.w...
KB4025342 2017-07 Cumulative Update for Windows 10 Version 1703 for x86-based Systems (KB... http://download.w...
This command find latest Windows 10 32-bit cumulative updates for all build numbers.
.EXAMPLE
Find-CumulativeWindowsUpdate -Build 14393
KB Description Uri
-- ----------- ---
KB4025339 2017-07 Cumulative Update for Windows Server 2016 for x64-based Systems (KB4025... http://download.w...
KB4025339 2017-07 Cumulative Update for Windows 10 Version 1607 for x64-based Systems (KB... http://download.w...
KB4025339 2017-07 Cumulative Update for Windows 10 Version 1607 for x86-based Systems (KB... http://download.w...
This command find latest cumulative updates for all the Windows 10/Server 2016 anniversary update.
.EXAMPLE
Find-CumulativeWindowsUpdate -Build ($PSVersionTable.BuildVersion.Build)
KB Description Uri
-- ----------- ---
KB4025342 2017-07 Cumulative Update for Windows 10 Version 1703 for x64-based Systems (KB... http://download.w...
KB4025342 2017-07 Cumulative Update for Windows 10 Version 1703 for x86-based Systems (KB... http://download.w...
This command finds the latest cumulative updates for the current OS build.
.NOTES
Adapted from https://github.com/aaronparker/MDT/blob/master/Updates/Get-LatestUpdate.ps1
#>
[CmdletBinding()]
param (
## Specifies the Windows 10/Server 2016 build number(s) used to find cumulative updates.
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.UInt32[]] $Build = @(10240,10586,14393,15063),
## Specifies a particular Operating System to search for.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('x86','x64')]
[System.String] $Architecture,
## Specifies to return delta updates rather than full cumulative updates.
[Parameter(ValueFromPipelineByPropertyName)]
[System.Management.Automation.SwitchParameter] $Delta
)
begin {
Write-Verbose -Message ("Downloading update KB articles");
$kbArticles = (Invoke-WebRequest -Uri 'https://support.microsoft.com/api/content/asset/4000816').Content |
ConvertFrom-Json;
if ($Delta) {
## Set searching to filter on delta updates
$downloadLinkRegex = 'Delta';
}
else {
## Default to searching only on cumulative update type
$downloadLinkRegex = 'Cumulative';
}
if ($PSBoundParameters.ContainsKey('Architecture')) {
## Update the regular expression to include an architecture
$downloadLinkRegex = '{0}.*{1}' -f $downloadLinkRegex, $Architecture;
}
} #end begin
process {
foreach ($buildNumber in $Build) {
Write-Verbose -Message ("Searching for updates matching build number '{0}'." -f $buildNumber);
# Find the latest KB article number
$kbArticle = $kbArticles | Select-Object -ExpandProperty Links |
Where-Object { $_.level -eq 2 -and $_.text -match "Build $buildNumber\." } |
Sort-Object -Property Id |
Select-Object -Last 1
if ($null -eq $kbArticle) {
Write-Warning -Message ("Could not find KB article(s) matching build number '{0}'." -f $buildNumber);
continue;
}
Write-Verbose -Message ("Found build number '{0}' KB article '{1}'." -f $buildNumber, $kbArticle.Text);
## Get the download links
Invoke-WebRequest -Uri "http://www.catalog.update.microsoft.com/Search.aspx?q=KB$($kbArticle.articleID)" |
Select -ExpandProperty Links |
Where-Object { $_.ID -match '_link' -and $_.OuterHTML -match $downloadLinkRegex } |
ForEach-Object -PipelineVariable downloadLink {
$kbId = $_.ID.TrimEnd('_link');
$kbDescription = $_.innerText;
$invokeWebRequestBody = @{
size = 0;
updateID = $kbId;
uidInfo = $kbId
} | ConvertTo-Json -Compress;
$invokeWebRequestParams = @{
Uri = 'http://www.catalog.update.microsoft.com/DownloadDialog.aspx';
Method = 'Post';
Body = @{ updateIDs = '[{0}]' -f $invokeWebRequestBody; }
}
Invoke-WebRequest @invokeWebRequestParams |
Select-Object -ExpandProperty Content |
Select-String -AllMatches -Pattern "(http[s]?\://download\.windowsupdate\.com\/[^\'\""]*)" |
ForEach-Object {
Write-Output -InputObject ([PSCustomObject] @{
KB = 'KB{0}' -f $kbArticle.ArticleId;
Description = $kbDescription;
Uri = $_.Matches.Value;
});
}
} #end foreach download link
} #end foreach build number
} #end process
} #end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment