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:
1 parent
96c689b03e
commit
26baf25172
2 files changed
+46
-7
No files matched your search
@@ -162,7 +162,7 @@
|
||||
<SynchronousCommand wcm:action="add">
|
||||
<Order>1</Order>
|
||||
<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.' }; & $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.' }; & $script.FullName -InstallVirtIO -EnableAutomaticLogon -NoPause"</CommandLine>
|
||||
<RequiresUserInput>false</RequiresUserInput>
|
||||
</SynchronousCommand>
|
||||
</FirstLogonCommands>
|
||||
|
||||
@@ -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) {
|
||||
$exitCode = 0
|
||||
try {
|
||||
if ($InstallVirtIO) {
|
||||
Install-VirtIOGuestTools
|
||||
}
|
||||
Install-OpenSSHServer
|
||||
Install-AdministratorAuthorizedKey
|
||||
if ($EnableAutomaticLogon) {
|
||||
}
|
||||
Install-OpenSSHServer
|
||||
Install-AdministratorAuthorizedKey
|
||||
if ($EnableAutomaticLogon) {
|
||||
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
|
||||
Reference in new issue
Block a user