Skip to content

Instantly share code, notes, and snippets.

@keithga
Last active February 23, 2024 13:17
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save keithga/1ad0abd1f7ba6e2f8aff63d94ab03048 to your computer and use it in GitHub Desktop.
Save keithga/1ad0abd1f7ba6e2f8aff63d94ab03048 to your computer and use it in GitHub Desktop.
script to get latest build.
<#
.SYNOPSIS
Get the latest Cumulative update for Windows
.DESCRIPTION
This script will return the list of Cumulative updates for Windows 10 and Windows Server 2016 from the Microsoft Update Catalog.
.NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.
.LINK
https://support.microsoft.com/en-us/help/4000823
.EXAMPLE
Get the latest Cumulative Update for Windows 10 x64
.\Get-LatestUpdate.ps1
.PARAMETER Build
Windows 10 Build Number used to filter avaible Downloads
10240 - Windows 10 Version 1507
10586 - Windows 10 Version 1511
14393 - Windows 10 Version 1607 and Windows Server 2016
15063 - Windows 10 Version 1703
16299 - WIndows 10 Version 1709
.PARAMETER Filter
Specify a specific search filter to change the target update behaviour. The default will only Cumulative updates for x86 and x64.
If Mulitple Filters are specified, only string that match *ALL* filters will be selected.
Cumulative - Download Cumulative updates.
Delta - Download Delta updates.
x86 - Download x86
x64 - Download x64
.EXAMPLE
Get the latest Cumulative Update for Windows 10 x86
.\Get-LatestUpdate.ps1 -Filter 'Cumulative','x86'
.EXAMPLE
Get the latest Cumulative Update for Windows Server 2016
.\Get-LatestUpdate.ps1 -Filter 'Cumulative','x64' -Build 14393
.EXAMPLE
Get the latest Cumulative Updates for Windows 10 (both x86 and x64) and download to the %TEMP% directory.
.\Get-LatestUpdate.ps1 | Start-BitsTransfer -Destination $env:Temp
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False, HelpMessage="JSON source for the update KB articles.")]
[string] $StartKB = 'https://support.microsoft.com/app/content/api/content/asset/en-us/4000816',
[Parameter(Mandatory=$False, HelpMessage="Windows build number.")]
[ValidateSet('16299','15063','14393','10586','10240')]
[string] $BUild = '16299',
[Parameter(Mandatory=$False, HelpMessage="Windows update Catalog Search Filter.")]
[ValidateSet('x64','x86','Cumulative','Delta',$null)]
[string[]] $Filter = @( "Cumulative" )
)
#region Support Routine
Function Select-LatestUpdate {
[CmdletBinding(SupportsShouldProcess=$True)]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
$Updates
)
Begin {
$MaxObject = $null
$MaxValue = [version]::new("0.0")
}
Process {
ForEach ( $Update in $updates ) {
Select-String -InputObject $Update -AllMatches -Pattern "(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)" |
ForEach-Object { $_.matches.value } |
ForEach-Object { $_ -as [version] } |
ForEach-Object {
if ( $_ -gt $MaxValue ) { $MaxObject = $Update; $MaxValue = $_ }
}
}
}
End {
$MaxObject | Write-Output
}
}
#endregion
#region Find the KB Article Number
Write-Verbose "Downloading $StartKB to retrieve the list of updates."
$kbID = Invoke-WebRequest -Uri $StartKB |
Select-Object -ExpandProperty Content |
ConvertFrom-Json |
Select-Object -ExpandProperty Links |
Where-Object level -eq 2 |
Where-Object text -match $BUild |
Select-LatestUpdate |
Select-Object -First 1
#endregion
#region get the download link from Windows Update
Write-Verbose "Found ID: KB$($kbID.articleID)"
$kbObj = Invoke-WebRequest -Uri "http://www.catalog.update.microsoft.com/Search.aspx?q=KB$($KBID.articleID)"
$Available_KBIDs = $kbObj.InputFields |
Where-Object { $_.type -eq 'Button' -and $_.Value -eq 'Download' } |
Select-Object -ExpandProperty ID
$Available_KBIDs | out-string | write-verbose
$kbGUIDs = $kbObj.Links |
Where-Object ID -match '_link' |
Where-Object { $_.OuterHTML -match ( "(?=.*" + ( $Filter -join ")(?=.*" ) + ")" ) } |
ForEach-Object { $_.id.replace('_link','') } |
Where-Object { $_ -in $Available_KBIDs }
foreach ( $kbGUID in $kbGUIDs )
{
Write-Verbose "`t`tDownload $kbGUID"
$Post = @{ size = 0; updateID = $kbGUID; uidInfo = $kbGUID } | ConvertTo-Json -Compress
$PostBody = @{ updateIDs = "[$Post]" }
Invoke-WebRequest -Uri 'http://www.catalog.update.microsoft.com/DownloadDialog.aspx' -Method Post -Body $postBody |
Select-Object -ExpandProperty Content |
Select-String -AllMatches -Pattern "(http[s]?\://download\.windowsupdate\.com\/[^\'\""]*)" |
Select-Object -Unique |
ForEach-Object { [PSCustomObject] @{ Source = $_.matches.value } } # Output for BITS
}
#endregion
@jm0477
Copy link

jm0477 commented Nov 11, 2018

Hi Keith, I found the startKB url https://support.microsoft.com/app/content/api/content/asset/en-us/4000816 is not updating since oct,2018. By using this script, we can only find the latest CU is October,2018. but cannot find anyone new.
I try to find the json format update history but no luck. could you shed some light please?

@Elevate08
Copy link

Hey Keith, Much like what jm0477 has mentioned above, I too have come across this script recently and noticed that for build 14393 the latest update the startKB url is showing is 8/2018 or KB KB4343887. The Latest Cumulative Update out today is KB4467684 with a build version of 14393.2639

I've tried locating a new API url but am not sure where you found that url. Would you be able to help me out in any way.

I'm looking into automating our image build process and this script along with one for service stacking updates would be crucial to getting the latest updates to slipstream into the iso.

@cdie2k
Copy link

cdie2k commented Jan 27, 2020

automating our image build process

Hello , did you found a way to automating the image/iso updates?

thanks

@cdie2k
Copy link

cdie2k commented Feb 13, 2020

found part of the solution, with "Install-Module -Name MSCatalog" we will have the option to download the msu. and there are others script to mount the iso and install the msu with dsim (I didnt test it, yet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment