-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun-WinServerSetup.ps1
More file actions
67 lines (58 loc) · 2.25 KB
/
Run-WinServerSetup.ps1
File metadata and controls
67 lines (58 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Run-WinServerSetup.ps1
# Launcher: right-click "Run with PowerShell" or run from any PowerShell window.
# Auto-elevates if not already running as Administrator. Forwards all switches
# to WinServerSetup.ps1 so callers can do e.g. `Run-WinServerSetup.ps1 -Full -NoPause`.
[CmdletBinding()]
param(
[switch]$Full,
[switch]$NoPause,
[switch]$NoColor,
[switch]$NoReboot,
[switch]$NoRelocate
)
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force | Out-Null
$scriptPath = Join-Path $PSScriptRoot "WinServerSetup.ps1"
function Read-AnyKeyExit {
param([string]$Prompt = "Press any key to exit...")
Write-Host $Prompt -ForegroundColor Yellow
try {
if ($Host.UI.RawUI -and [Console]::IsInputRedirected -eq $false) {
[void][Console]::ReadKey($true)
return
}
} catch {
Write-Verbose ("ReadKey fallback: {0}" -f $_.Exception.Message)
}
Read-Host | Out-Null
}
if (-not (Test-Path $scriptPath)) {
Write-Host "WinServerSetup.ps1 was not found next to this launcher." -ForegroundColor Red
Read-AnyKeyExit
exit 1
}
function Test-IsElevated {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
$childArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$scriptPath`"")
if ($Full) { $childArgs += "-Full" }
if ($NoPause) { $childArgs += "-NoPause" }
if ($NoColor) { $childArgs += "-NoColor" }
if ($NoReboot) { $childArgs += "-NoReboot" }
if ($NoRelocate) { $childArgs += "-NoRelocate" }
if (Test-IsElevated) {
# Already elevated: run in this window so logs are visible inline.
& powershell.exe @childArgs
exit $LASTEXITCODE
}
# Not elevated: relaunch elevated. -Verb RunAs triggers UAC.
try {
$elevated = Start-Process powershell.exe -ArgumentList $childArgs -Verb RunAs -Wait -PassThru
if ($null -ne $elevated.ExitCode) { exit $elevated.ExitCode }
} catch {
Write-Host "Failed to elevate. Please right-click PowerShell and choose 'Run as Administrator'." -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Read-AnyKeyExit
exit 1
}