From fe2d51f44efd22a115d25d6a494d58e8f99833ff Mon Sep 17 00:00:00 2001 From: Simone Date: Mon, 11 May 2026 21:11:21 +0200 Subject: [PATCH] feat(template): reboot VM before final shutdown+snapshot --- template/Prepare-WinBuild2022.ps1 | 40 +++++++++++++++++++++++++++++++ template/Prepare-WinBuild2025.ps1 | 20 ++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/template/Prepare-WinBuild2022.ps1 b/template/Prepare-WinBuild2022.ps1 index 3febeab..2181950 100644 --- a/template/Prepare-WinBuild2022.ps1 +++ b/template/Prepare-WinBuild2022.ps1 @@ -132,6 +132,18 @@ param( [string] $DotNetSdkVersion = '10.0', + # Tier-2 Toolchain version pins — forwarded as-is to Setup-WinBuild2022.ps1 + [string] $Pwsh7Version = '7.6.1', + [string] $NSISVersion = '3.12', + [string] $CMakeVersion = '3.31.6', + [string] $NodeJSVersion = '22.14.0', + [string] $WiXVersion = '4.0.5', + [string] $GhCliVersion = '2.92.0', + [string] $VcpkgRef = '', + + # Skip Tier-2 Toolchain — forwarded to Setup-WinBuild2022.ps1 -SkipTier2 + [switch] $SkipTier2, + [switch] $SkipWindowsUpdate, # Skip disk cleanup inside the VM (cleanmgr + DISM + cache clear) — speeds up re-runs @@ -368,9 +380,17 @@ try { BuildUsername = $BuildUsername DotNetSdkVersion = $DotNetSdkVersion AdminPassword = $AdminPassword + Pwsh7Version = $Pwsh7Version + NSISVersion = $NSISVersion + CMakeVersion = $CMakeVersion + NodeJSVersion = $NodeJSVersion + WiXVersion = $WiXVersion + GhCliVersion = $GhCliVersion + VcpkgRef = $VcpkgRef } if ($SkipWindowsUpdate) { $setupArgs['SkipWindowsUpdate'] = $true } if ($SkipCleanup) { $setupArgs['SkipCleanup'] = $true } + if ($SkipTier2) { $setupArgs['SkipTier2'] = $true } # ── Run Setup-WinBuild2022.ps1 inside the VM ────────────────────────────── # Setup may return exit 3010 (ERROR_SUCCESS_REBOOT_REQUIRED) when Windows @@ -570,6 +590,26 @@ try { $choice = Read-Host "[Prepare] Shut down VM and create snapshot '$SnapshotName'? [Y/N]" if ($choice -match '^[Yy]') { + # Reboot VM to flush any pending install state before final snapshot + Write-Host "[Prepare] Rebooting VM to finalise installation state..." + try { + Invoke-Command -Session $session -ScriptBlock { Restart-Computer -Force } -ErrorAction SilentlyContinue + } + catch { } + Remove-PSSession $session -ErrorAction SilentlyContinue + $session = $null + + Start-Sleep -Seconds 30 # let WinRM tear down before polling + Write-Host "[Prepare] Waiting for guest WinRM to come back after reboot (max 10 min)..." + if (-not (Wait-GuestWinRMReady -IP $VMIPAddress -Cred $credential ` + -SessionOptions $sessionOptions -TimeoutMin 10)) { + throw "[Prepare] Guest did not come back after reboot within 10 min." + } + Write-Host "[Prepare] Guest WinRM ready after reboot." -ForegroundColor Green + $session = New-PSSession -ComputerName $VMIPAddress -Credential $credential ` + -SessionOption $sessionOptions -UseSSL -Port 5986 ` + -Authentication Basic -ErrorAction Stop + # Send graceful shutdown Write-Host "[Prepare] Sending graceful shutdown to VM..." try { diff --git a/template/Prepare-WinBuild2025.ps1 b/template/Prepare-WinBuild2025.ps1 index 0ef1de0..cd3bb5b 100644 --- a/template/Prepare-WinBuild2025.ps1 +++ b/template/Prepare-WinBuild2025.ps1 @@ -590,6 +590,26 @@ try { $choice = Read-Host "[Prepare] Shut down VM and create snapshot '$SnapshotName'? [Y/N]" if ($choice -match '^[Yy]') { + # Reboot VM to flush any pending install state before final snapshot + Write-Host "[Prepare] Rebooting VM to finalise installation state..." + try { + Invoke-Command -Session $session -ScriptBlock { Restart-Computer -Force } -ErrorAction SilentlyContinue + } + catch { } + Remove-PSSession $session -ErrorAction SilentlyContinue + $session = $null + + Start-Sleep -Seconds 30 # let WinRM tear down before polling + Write-Host "[Prepare] Waiting for guest WinRM to come back after reboot (max 10 min)..." + if (-not (Wait-GuestWinRMReady -IP $VMIPAddress -Cred $credential ` + -SessionOptions $sessionOptions -TimeoutMin 10)) { + throw "[Prepare] Guest did not come back after reboot within 10 min." + } + Write-Host "[Prepare] Guest WinRM ready after reboot." -ForegroundColor Green + $session = New-PSSession -ComputerName $VMIPAddress -Credential $credential ` + -SessionOption $sessionOptions -UseSSL -Port 5986 ` + -Authentication Basic -ErrorAction Stop + # Send graceful shutdown Write-Host "[Prepare] Sending graceful shutdown to VM..." try {