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 <noreply@anthropic.com>
This commit is contained in:
GuofangandClaude Sonnet 5.5 committed 2026-10-15 10:02:37 +08:00
1 parent 96c689b03e
commit 26baf25172
2 files changed
+46 -7

No files matched your search

+1 -1
View File
@@ -162,7 +162,7 @@
<SynchronousCommand wcm:action="add"> <SynchronousCommand wcm:action="add">
<Order>1</Order> <Order>1</Order>
<Description>Configure Windows from the answer-media script</Description> <Description>Configure Windows from the answer-media script</Description>
<CommandLine>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.' }; &amp; $script.FullName -InstallVirtIO -EnableAutomaticLogon"</CommandLine> <CommandLine>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.' }; &amp; $script.FullName -InstallVirtIO -EnableAutomaticLogon -NoPause"</CommandLine>
<RequiresUserInput>false</RequiresUserInput> <RequiresUserInput>false</RequiresUserInput>
</SynchronousCommand> </SynchronousCommand>
</FirstLogonCommands> </FirstLogonCommands>
+45 -6
View File
@@ -1,11 +1,37 @@
param( param(
[switch]$InstallVirtIO, [switch]$InstallVirtIO,
[switch]$EnableAutomaticLogon [switch]$EnableAutomaticLogon,
[switch]$NoPause
) )
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest 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 { function Install-VirtIOGuestTools {
$installer = Get-ChildItem ` $installer = Get-ChildItem `
-Path (Get-PSDrive -PSProvider FileSystem).Root ` -Path (Get-PSDrive -PSProvider FileSystem).Root `
@@ -96,11 +122,24 @@ function Enable-AutomaticLogon {
Remove-ItemProperty -Path $winlogonPath -Name 'AutoLogonCount' -ErrorAction SilentlyContinue Remove-ItemProperty -Path $winlogonPath -Name 'AutoLogonCount' -ErrorAction SilentlyContinue
} }
if ($InstallVirtIO) { $exitCode = 0
try {
if ($InstallVirtIO) {
Install-VirtIOGuestTools Install-VirtIOGuestTools
} }
Install-OpenSSHServer Install-OpenSSHServer
Install-AdministratorAuthorizedKey Install-AdministratorAuthorizedKey
if ($EnableAutomaticLogon) { if ($EnableAutomaticLogon) {
Enable-AutomaticLogon Enable-AutomaticLogon
}
Write-Host 'Done.' -ForegroundColor Green
} }
catch {
Write-Host "Failed: $($_.Exception.Message)" -ForegroundColor Red
$exitCode = 1
}
if (-not $NoPause) {
Read-Host 'Press Enter to exit' | Out-Null
}
exit $exitCode