feat(template): reboot VM before final shutdown+snapshot

This commit is contained in:
Simone
2026-05-11 21:11:21 +02:00
parent b89fc7340d
commit fe2d51f44e
2 changed files with 60 additions and 0 deletions
+20
View File
@@ -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 {