I'm currently working on an automated build of a Windows 11 24H2 image using Packer. I found that the Windows update plugin doesn't function when building an image in Audit mode, so I'm looking for alternative methods to install Windows updates. I've learned that I can't just run a PowerShell script with PSWindowsUpdates directly; I need to use Invoke-WUJob instead. I've managed to automate the installation of the module, but when I try to execute my Invoke-WUJob script, it creates a scheduled task that just stays in a 'Queued' state. I'm not sure where I went wrong and would appreciate any insights. Here's the code I'm using: Invoke-WUJob -ComputerName localhost -RunNow -Confirm:$false -Script { Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -ForceDownload -ForceInstall -IgnoreReboot -Category 'Security' } Get-ScheduledTask -TaskName 'PSWindowsUpdate' do { $scheduledTask = Get-ScheduledTask -TaskName 'PSWindowsUpdate' Write-Host 'PSWindowsUpdate task: $($scheduledTask.State)' Start-Sleep -Seconds 10 } while ($scheduledTask.State -ne 'Ready') $taskExists = Get-ScheduledTask -TaskName 'PSWindowsUpdate' if ($taskExists) { Get-ScheduledTask -TaskName 'PSWindowsUpdate' Unregister-ScheduledTask -TaskName 'PSWindowsUpdate' -Confirm:$false } else { Write-Host 'PSWindowsUpdate isn't listed as a Scheduled Task.' }
5 Answers
This doesn't directly fix your issue, but try not to use back ticks for line continuation; they can lead to confusion in scripts. The blog I read suggests alternative ways to format your code. You might want to consider that.
If you're troubleshooting scripts that aren't interactive, I recommend using the Start-Transcript command to log the output to a file. This way, you can see what happens during the execution. You can use Get-Content with -Wait to continuously check the file for updates instead of repetitively opening it in Notepad. Good luck with your build!
Have you tried running your code outside of Audit mode? It sometimes helps to isolate the issue. Also, consider adding some logging to confirm whether the problem is with the loop or if Windows Update isn't running at all.
I'm with you on the back ticks. I know they're meant for readability, but honestly, they can complicate things. Splatting might be a more functional approach that allows you to format and manipulate your scripts more easily.
Instead of using PSWindowsUpdate, you could leverage DISM to install updates. You can call dism.exe directly or use a PowerShell module that wraps around it. It might simplify your process.

Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures