From 26baf251726d4c03c312fd5cb2b547c56f9cf8aa Mon Sep 17 00:00:00 2001 From: Guofang Date: Thu, 15 Oct 2026 10:02:37 +0800 Subject: [PATCH] Self-elevate and pause at the end of the Windows 11 configure script Relaunch as Administrator through UAC when needed, report success or failure, and wait for Enter before the window closes. Autounattend.xml passes -NoPause so the unattended install does not block. Co-Authored-By: Claude Sonnet 5.5 --- seediso/windows11/Autounattend.xml | 2 +- seediso/windows11/Configure-Windows.ps1 | 53 +++++++++++++++++++++---- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/seediso/windows11/Autounattend.xml b/seediso/windows11/Autounattend.xml index 95edb89..6a42b19 100644 --- a/seediso/windows11/Autounattend.xml +++ b/seediso/windows11/Autounattend.xml @@ -162,7 +162,7 @@ 1 Configure Windows from the answer-media script - powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$script = Get-ChildItem -Path (Get-PSDrive -PSProvider FileSystem).Root -Filter 'Configure-Windows.ps1' -File -ErrorAction SilentlyContinue | Select-Object -First 1; if (-not $script) { throw 'Configure-Windows.ps1 was not found on the attached media.' }; & $script.FullName -InstallVirtIO -EnableAutomaticLogon" + powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$script = Get-ChildItem -Path (Get-PSDrive -PSProvider FileSystem).Root -Filter 'Configure-Windows.ps1' -File -ErrorAction SilentlyContinue | Select-Object -First 1; if (-not $script) { throw 'Configure-Windows.ps1 was not found on the attached media.' }; & $script.FullName -InstallVirtIO -EnableAutomaticLogon -NoPause" false diff --git a/seediso/windows11/Configure-Windows.ps1 b/seediso/windows11/Configure-Windows.ps1 index 91295f1..4c1fc79 100644 --- a/seediso/windows11/Configure-Windows.ps1 +++ b/seediso/windows11/Configure-Windows.ps1 @@ -1,11 +1,37 @@ param( [switch]$InstallVirtIO, - [switch]$EnableAutomaticLogon + [switch]$EnableAutomaticLogon, + [switch]$NoPause ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest +function Test-Administrator { + $identity = [Security.Principal.WindowsIdentity]::GetCurrent() + $principal = [Security.Principal.WindowsPrincipal]::new($identity) + return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +} + +if (-not (Test-Administrator)) { + if (-not $PSCommandPath) { + throw 'Run this script from a PowerShell window opened as Administrator.' + } + + $arguments = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$PSCommandPath`"") + foreach ($name in $PSBoundParameters.Keys) { + $arguments += "-$name" + } + + $elevated = Start-Process ` + -FilePath 'powershell.exe' ` + -ArgumentList $arguments ` + -Verb RunAs ` + -Wait ` + -PassThru + exit $elevated.ExitCode +} + function Install-VirtIOGuestTools { $installer = Get-ChildItem ` -Path (Get-PSDrive -PSProvider FileSystem).Root ` @@ -96,11 +122,24 @@ function Enable-AutomaticLogon { Remove-ItemProperty -Path $winlogonPath -Name 'AutoLogonCount' -ErrorAction SilentlyContinue } -if ($InstallVirtIO) { - Install-VirtIOGuestTools +$exitCode = 0 +try { + if ($InstallVirtIO) { + Install-VirtIOGuestTools + } + Install-OpenSSHServer + Install-AdministratorAuthorizedKey + if ($EnableAutomaticLogon) { + Enable-AutomaticLogon + } + Write-Host 'Done.' -ForegroundColor Green } -Install-OpenSSHServer -Install-AdministratorAuthorizedKey -if ($EnableAutomaticLogon) { - Enable-AutomaticLogon +catch { + Write-Host "Failed: $($_.Exception.Message)" -ForegroundColor Red + $exitCode = 1 } + +if (-not $NoPause) { + Read-Host 'Press Enter to exit' | Out-Null +} +exit $exitCode