Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Calculate Patch Tuesday
- $PatchTuesday = Get-Date -Day 8
- while ($PatchTuesday.DayOfWeek -ne 'Tuesday') {
- $PatchTuesday = $PatchTuesday.AddDays(1)
- }
- $osVersionShortName = @{
- "26100" = "24H2"
- "22631" = "23H2"
- "22621" = "22H2"
- "19045" = "22H2"
- "19044" = "21H2"
- }
- <# # This was from reading information from a wim
- $WindowsType = $wimImageInfo.ImageDescription.Substring(8,2)
- $windowsVersion = $osVersionShortName[$wimImageInfo.Build]
- $windowsArch = if ($wimImageInfo.Architecture -eq 9) { "x64" } else { "x86" }
- #>
- $hostInfo = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion") | Select-Object -Property *
- $windowsType = $hostInfo.CurrentMajorVersionNumber
- $windowsVersion = $osVersionShortName[$hostInfo.CurrentBuild] # You could also use DisplayVerison here
- $windowsArch = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" }
- # Are we before or after patch tuesday
- If ((Get-Date) -lt $PatchTuesday)
- {
- If ($PatchTuesday.Month -ne "1")
- {
- $search = "{0:d4}-{1:d2} Cumulative Update for Windows {2} Version {3} for {4}" -f $PatchTuesday.Year, $PatchTuesday.AddMonths(-1).Month, $WindowsType, $windowsVersion, $windowsArch
- }
- else
- {
- $search = "{0:d4}-{1:d2} Cumulative Update for Windows {2} Version {3} for {4}" -f $PatchTuesday.AddYears(-1).Year, $PatchTuesday.AddMonths(-1).Month, $WindowsType, $windowsVersion, $windowsArch
- }
- }
- else
- {
- $search = "{0:d4}-{1:d2} Cumulative Update for Windows {2} Version {3} for {4}" -f $PatchTuesday.Year, $PatchTuesday.Month, $WindowsType, $windowsVersion, $windowsArch
- }
- # MS Update Catalog search url
- $catalog = "https://www.catalog.update.microsoft.com/Search.aspx?q="
- # We can search for .net updates later
- # In the format 2025-03 Cumulative Update for Microsoft .NET Framework Windows 11 version 24H2 x64
- # Completed search string
- $search = $catalog + '"' + $search + '"'
- # Capture the download page so we can get the update GUID
- $results = Invoke-WebRequest -Uri $search -UseBasicParsing
- # Gets the guid of the update
- $kbids = $results.InputFields | Where-Object { $_.type -eq 'Button' -and $_.Value -eq 'Download' } | Select-Object -ExpandProperty ID
- $guids = $results.Links | Where-Object ID -match '_link' | Where-Object { $_.OuterHTML -match ( "(?=.*" + ( $Filter -join ")(?=.*" ) + ")" ) } | ForEach-Object { $_.id.replace('_link', '') } | Where-Object { $_ -in $kbids }
- # Send the guid information as a request to the download page
- $post = @{ size = 0; languages = ""; uidInfo = $guids; updateID = $guids } | ConvertTo-Json -Compress
- $body = @{ updateIDs = "[$post]" }
- $catDLPage = Invoke-WebRequest -Uri 'https://www.catalog.update.microsoft.com/DownloadDialog.aspx' -Method Post -Body $body -UseBasicParsing
- If ($catDLPage.StatusCode -eq "200") {
- # We read the raw html, which sucks ass
- $htmlParse = ($catDLPage | Select-Object -ExpandProperty Content) -split "[`r`n]"
- # Find the line with the adctual download link
- $junk = $htmlParse | Select-String "https://catalog.sf.dl.delivery.mp.microsoft.com"
- If ($junk -eq $null) {
- Write-Warning "Unable to find download URL https://catalog.sf.dl.delivery.mp.microsoft.com"
- }
- foreach ($item in $junk) {
- # Get the download link from the parsed lines
- $link = $item.Line.Substring($item.Matches.Index, ($item.Line.Length - $item.Matches.Index - 2))
- $filename = $link.Substring($link.IndexOf("windows" + $WindowsType))
- # Download the file
- if ($link -match "kb5043080") {
- # Hacky garbage, this keeps showing up since september but the KB is a patch from 2024-09
- } else {
- Invoke-WebRequest -Uri $link -OutFile ("$PSScriptRoot\$filename")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement