Advertisement
Guest User

Untitled

a guest
Apr 10th, 2025
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Calculate Patch Tuesday
  2. $PatchTuesday = Get-Date -Day 8
  3. while ($PatchTuesday.DayOfWeek -ne 'Tuesday') {
  4.     $PatchTuesday = $PatchTuesday.AddDays(1)
  5. }
  6.  
  7. $osVersionShortName = @{
  8. "26100" = "24H2"
  9. "22631" = "23H2"
  10. "22621" = "22H2"
  11. "19045" = "22H2"
  12. "19044" = "21H2"
  13. }
  14.  
  15. <#    # This was from reading information from a wim
  16. $WindowsType = $wimImageInfo.ImageDescription.Substring(8,2)
  17. $windowsVersion = $osVersionShortName[$wimImageInfo.Build]
  18. $windowsArch = if ($wimImageInfo.Architecture -eq 9) { "x64" } else { "x86" }
  19. #>
  20. $hostInfo = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion") | Select-Object -Property *
  21. $windowsType = $hostInfo.CurrentMajorVersionNumber
  22. $windowsVersion = $osVersionShortName[$hostInfo.CurrentBuild]  # You could also use DisplayVerison here
  23. $windowsArch = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" }
  24.    
  25. # Are we before or after patch tuesday
  26. If ((Get-Date) -lt $PatchTuesday)
  27. {
  28.     If ($PatchTuesday.Month -ne "1")
  29.     {
  30.         $search = "{0:d4}-{1:d2} Cumulative Update for Windows {2} Version {3} for {4}" -f $PatchTuesday.Year, $PatchTuesday.AddMonths(-1).Month, $WindowsType, $windowsVersion, $windowsArch
  31.     }
  32.     else
  33.     {
  34.         $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
  35.  
  36.     }
  37. }
  38. else
  39. {
  40.     $search = "{0:d4}-{1:d2} Cumulative Update for Windows {2} Version {3} for {4}" -f $PatchTuesday.Year, $PatchTuesday.Month, $WindowsType, $windowsVersion, $windowsArch
  41. }
  42.  
  43. # MS Update Catalog search url
  44. $catalog = "https://www.catalog.update.microsoft.com/Search.aspx?q="
  45.  
  46. # We can search for .net updates later
  47. # In the format 2025-03 Cumulative Update for Microsoft .NET Framework Windows 11 version 24H2 x64
  48.  
  49. # Completed search string
  50. $search = $catalog + '"' + $search + '"'
  51.  
  52. # Capture the download page so we can get the update GUID
  53. $results = Invoke-WebRequest -Uri $search -UseBasicParsing
  54.  
  55. # Gets the guid of the update
  56. $kbids = $results.InputFields | Where-Object { $_.type -eq 'Button' -and $_.Value -eq 'Download' } | Select-Object -ExpandProperty  ID
  57. $guids = $results.Links | Where-Object ID -match '_link' | Where-Object { $_.OuterHTML -match ( "(?=.*" + ( $Filter -join ")(?=.*" ) + ")" ) } | ForEach-Object { $_.id.replace('_link', '') } | Where-Object { $_ -in $kbids }
  58.  
  59.  
  60. # Send the guid information as a request to the download page
  61. $post = @{ size = 0; languages = ""; uidInfo = $guids; updateID = $guids } | ConvertTo-Json -Compress
  62. $body = @{ updateIDs = "[$post]" }
  63. $catDLPage = Invoke-WebRequest -Uri 'https://www.catalog.update.microsoft.com/DownloadDialog.aspx' -Method Post -Body $body -UseBasicParsing
  64.  
  65. If ($catDLPage.StatusCode -eq "200") {
  66.     # We read the raw html, which sucks ass
  67.     $htmlParse = ($catDLPage | Select-Object -ExpandProperty Content) -split "[`r`n]"
  68.  
  69.     # Find the line with the adctual download link
  70.     $junk = $htmlParse | Select-String "https://catalog.sf.dl.delivery.mp.microsoft.com"
  71.  
  72.     If ($junk -eq $null) {
  73.         Write-Warning "Unable to find download URL https://catalog.sf.dl.delivery.mp.microsoft.com"
  74.     }
  75.  
  76.     foreach ($item in $junk) {
  77.         # Get the download link from the parsed lines
  78.         $link = $item.Line.Substring($item.Matches.Index, ($item.Line.Length - $item.Matches.Index - 2))
  79.         $filename = $link.Substring($link.IndexOf("windows" + $WindowsType))
  80.  
  81.         # Download the file
  82.         if ($link -match "kb5043080") {
  83.             # Hacky garbage, this keeps showing up since september but the KB is a patch from 2024-09
  84.         } else {
  85.                
  86.             Invoke-WebRequest -Uri $link -OutFile ("$PSScriptRoot\$filename")
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement