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