2019-12-13 09:48:00 -05:00
################################################################################
## File: Install-WindowsUpdates.ps1
## Desc: Install Windows Updates.
2020-06-22 01:25:04 -07:00
## Should be run at end, just before SoftwareReport and Finalize-VM.ps1.
2019-12-13 09:48:00 -05:00
################################################################################
2021-11-16 17:14:17 +03:00
function Install-WindowsUpdates {
Write-Host " Starting wuauserv "
Start-Service -Name wuauserv -PassThru | Out-Host
2024-01-11 19:25:36 +01:00
# Temporarily exclude Windows update KB5034439 since it throws an error.
# The known issue (https://support.microsoft.com/en-au/topic/kb5034439-windows-recovery-environment-update-for-azure-stack-hci-version-22h2-and-windows-server-2022-january-9-2024-6f9d26e6-784c-4503-a3c6-0beedda443ca)
2021-11-16 17:14:17 +03:00
Write-Host " Getting list of available windows updates "
2024-01-11 19:25:36 +01:00
Get-WindowsUpdate -MicrosoftUpdate -NotKBArticleID " KB5034439 " -OutVariable updates | Out-Host
2021-11-16 17:14:17 +03:00
if ( -not $updates ) {
Write-Host " There are no windows updates to install "
return
}
Write-Host " Installing windows updates "
2024-01-11 19:25:36 +01:00
Get-WindowsUpdate -MicrosoftUpdate -NotKBArticleID " KB5034439 " -AcceptAll -Install -IgnoreUserInput -IgnoreReboot | Out-Host
2021-11-16 17:14:17 +03:00
2023-11-27 12:29:42 +01:00
Write-Host " Validating windows updates installation "
2021-11-16 17:14:17 +03:00
# Get-WUHistory doesn't support Windows Server 2022
2023-11-27 12:29:42 +01:00
$notFailedUpdateNames = Get-WindowsUpdateStates | Where-Object { $_ . State -in ( " Installed " , " Running " ) } | Select-Object -ExpandProperty Title
# We ignore Microsoft Defender Antivirus updates; Azure service updates AV automatically
$failedUpdates = $updates [ 0 ] | Where-Object Title -notmatch " Microsoft Defender Antivirus " | Where-Object { -not ( $notFailedUpdateNames -match $_ . KB ) }
2021-11-16 17:14:17 +03:00
2023-11-27 12:29:42 +01:00
if ( $failedUpdates ) {
2023-12-04 10:50:53 +01:00
throw " Windows updates failed to install: $( $failedUpdates . KB ) "
2021-11-16 17:14:17 +03:00
}
}
Install-WindowsUpdates
# Create complete windows update file
New-Item -Path $env:windir -Name WindowsUpdateDone . txt -ItemType File | Out-Null