Last active
July 15, 2021 18:46
-
-
Save jrdnr/0f7be96ef6050f5cdd499a019a8792f7 to your computer and use it in GitHub Desktop.
Download and Install MSU from Hashtable based on OS version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # CU KBs and DL links https://www.catalog.update.microsoft.com/Search.aspx?q=2021-03%20cumulative%20update | |
| #Optional Runtime Variable $AutoReboot False by default. | |
| "`$AutoReboot set to '$AutoReboot'" | |
| # Get Windows Version | |
| $winVer = Get-ItemProperty -Path "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion" | | |
| Select-Object -Property ReleaseID,@{ | |
| n='Version'; e={[System.Version]('{0}.{1}.{2}' -f [int]$_.CurrentMajorVersionNumber,[int]$_CurrentMinorVersionNumber,[int]$_.CurrentBuildNumber)}} | |
| $kbs = @{ | |
| # Server 2016 | |
| '1607' = [PSCustomObject]@{ KB = 'KB5001633'; uri = 'http://download.windowsupdate.com/d/msdownload/update/software/updt/2021/03/windows10.0-kb5001633-x64_de4efdfb38b1229ba80d5f66689b9079a0871585.msu'} | |
| # Server 2019 | |
| '1809' = [PSCustomObject]@{ KB = 'KB5001638'; uri = 'http://download.windowsupdate.com/d/msdownload/update/software/updt/2021/03/windows10.0-kb5001638-x64_64937e493ea9574759536d4b2695c05dfa5543e3.msu'} | |
| '1903' = [PSCustomObject]@{ KB = 'KB5001566'; uri = 'http://download.windowsupdate.com/d/msdownload/update/software/updt/2021/03/windows10.0-kb5001566-x64_b52b66b45562d5a620a6f1a5e903600693be1de0.msu'} | |
| '1909' = [PSCustomObject]@{ KB = 'KB5001648'; uri = 'http://download.windowsupdate.com/d/msdownload/update/software/updt/2021/03/windows10.0-kb5001648-x64_770175898c1bee1cfbcadc981fa47797c46f8b09.msu'} | |
| '2004' = [PSCustomObject]@{ KB = 'KB5001649'; uri = 'http://download.windowsupdate.com/d/msdownload/update/software/updt/2021/03/windows10.0-kb5001649-x64_aca549448414a5ad559c742c39e9342468a23eb5.msu'} | |
| '2009' = [PSCustomObject]@{ KB = 'KB5001649'; uri = 'http://download.windowsupdate.com/d/msdownload/update/software/updt/2021/03/windows10.0-kb5001649-x64_aca549448414a5ad559c742c39e9342468a23eb5.msu'} | |
| } | |
| $fileName = ($Kbs[$winVer.ReleaseId].KB + '.msu') | |
| $KbMsu = Join-Path -Path "$env:SystemRoot\temp" -ChildPath $fileName | |
| if ((Get-HotFix).HotFixID -notcontains $Kbs[$winVer.Releaseid].KB) { | |
| "Downloading $fileName" | |
| try { | |
| Start-BitsTransfer -Source $kbs[$winVer.Releaseid].uri -Destination $KbMsu -ErrorAction Stop | |
| } | |
| catch [System.Exception] { | |
| Write-Warning -Message $Error[0].Exception.Message | |
| Write-Warning -Message 'Please check your patch URL and try again' | |
| exit 1 | |
| } | |
| catch { | |
| Write-Warning -Message "Error Downloading MSU" | |
| Write-Warning -Message (($Error[0] | Select-Object -Property Exception | Out-String).trim() -split '\r')[-1].Trim() | |
| exit 1 | |
| } | |
| "Starting $filename install." | |
| $ArgList = "$KbMsu /quiet /norestart" | |
| if ($AutoReboot -match '\$?true|1|yes'){ | |
| $ArgList = $ArgList.Replace(' /norestart','') | |
| } | |
| Start-Process -FilePath wusa -ArgumentList $ArgList -NoNewWindow | |
| } else { | |
| if (Test-Path -Path "$env:SystemRoot\temp\*.msu"){ | |
| Remove-Item -Path "$env:SystemRoot\temp\*.msu" | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to catch download errors