fix(elevation): quote script path and parameter values in admin relaunch (#683)

Co-authored-by: Jeffrey <9938813+Raphire@users.noreply.github.com>
This commit is contained in:
HetCreep
2026-07-04 17:35:24 +02:00
committed by GitHub
co-authored by Jeffrey
parent a9a89c1f13
commit 0eeb8f323a
+12 -3
View File
@@ -111,7 +111,14 @@ if (-not $isAdmin) {
$choice = Read-Host "Restart as Administrator? (y/n)"
if ($choice -match '^[Yy]$') {
$elevatedArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", $PSCommandPath)
# Win32-safe escaping for arguments to pass to elevated process
function Format-ElevatedArg([string]$Value) {
$escaped = $Value -replace '(\\*)"', '$1$1\"'
$escaped = $escaped -replace '(\\+)$', '$1$1'
return '"' + $escaped + '"'
}
$elevatedArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", (Format-ElevatedArg $PSCommandPath))
foreach ($paramName in $PSBoundParameters.Keys) {
$paramValue = $PSBoundParameters[$paramName]
@@ -123,12 +130,14 @@ if (-not $isAdmin) {
}
else {
$elevatedArgs += "-$paramName"
$elevatedArgs += "$paramValue"
$elevatedArgs += (Format-ElevatedArg $paramValue)
}
}
if ($MyInvocation.UnboundArguments.Count -gt 0) {
$elevatedArgs += $MyInvocation.UnboundArguments
foreach ($unboundArg in $MyInvocation.UnboundArguments) {
$elevatedArgs += (Format-ElevatedArg "$unboundArg")
}
}
Start-Process powershell -ArgumentList $elevatedArgs -Verb RunAs