mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-08-23 08:02:07 +00:00
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:
+12
-3
@@ -111,7 +111,14 @@ if (-not $isAdmin) {
|
|||||||
$choice = Read-Host "Restart as Administrator? (y/n)"
|
$choice = Read-Host "Restart as Administrator? (y/n)"
|
||||||
|
|
||||||
if ($choice -match '^[Yy]$') {
|
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) {
|
foreach ($paramName in $PSBoundParameters.Keys) {
|
||||||
$paramValue = $PSBoundParameters[$paramName]
|
$paramValue = $PSBoundParameters[$paramName]
|
||||||
@@ -123,12 +130,14 @@ if (-not $isAdmin) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$elevatedArgs += "-$paramName"
|
$elevatedArgs += "-$paramName"
|
||||||
$elevatedArgs += "$paramValue"
|
$elevatedArgs += (Format-ElevatedArg $paramValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($MyInvocation.UnboundArguments.Count -gt 0) {
|
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
|
Start-Process powershell -ArgumentList $elevatedArgs -Verb RunAs
|
||||||
|
|||||||
Reference in New Issue
Block a user