Connect-ITDvCenter -Credential $PrvCred $GuestCredential = Get-Secret -Name GuestCredential $AllNames = "Windows Server 2019*" #$AllNames = "zm2019" Get-Template -Name $AllNames | Where-Object {$_.Name -notlike "*(Base)*"} | Set-Template -ToVM #Get-VM -Name $AllNames | New-Snapshot -Name "0_Before Testing" $VMs = Get-VM -Name $AllNames -server itdvmvc1.nd.gov | Where-Object PowerState -ne "PoweredOn" | Start-VM Get-VM -Name $AllNames | Wait-Tools | Out-Null Write-Warning "Tools started" while ((Get-VM -Name $AllNames).ExtensionData.Guest.GuestOperationsReady -contains $false){start-sleep -seconds 5} Write-Warning "Guest Operations Ready" $PowerCliContext = Get-PowerCLIContext Write-Warning "Wait for 3 minutes" start-sleep -seconds 180 Write-Warning "Wait complete" Get-VM -Name $AllNames | ForEach-Object -Parallel { Use-PowerCLIContext -PowerCLIContext $using:PowerCliContext -SkipImportModuleChecks Get-VM -Name $_.Name | Wait-Tools $WUfunc = { Write-Host "Search for relevant updates" $Updates = New-Object -ComObject Microsoft.Update.UpdateColl $Searcher = New-Object -ComObject Microsoft.Update.Searcher $Session = New-Object -ComObject Microsoft.Update.Session $Criteria = "IsInstalled=0 and Type='Software' and IsAssigned=1" #IsAssigned Finds updates that are intended for deployment by Automatic Updates. This stops Preview updates for Windows and .NET # https://docs.microsoft.com/en-us/windows/win32/api/wuapi/nf-wuapi-iupdatesearcher-search $Result = $Searcher.Search($Criteria) $Updates = $Result.Updates $Updates | Sort-Object Title | Select-Object Title $Downloader = $Session.CreateUpdateDownloader() $Downloader.Updates = $Updates $Downloader.Download() $Installer = New-Object -ComObject Microsoft.Update.Installer $Installer.Updates = $Updates $Installer.Install() #> } Get-VM -Name $_.Name | Invoke-VMScript -GuestCredential $using:GuestCredential -ScriptType PowerShell -ScriptText $WUfunc Write-Warning ($_.Name + " updates completed, needs reboot...") } Get-VM -Name $_.Name | Restart-VMGuest -Confirm:$false | Out-Null Get-VM -Name $AllNames | Set-VM -ToTemplate