From af57949bf57ed2a1a1f2938781d847c924d25e0e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 28 Sep 2024 03:40:45 +0200 Subject: [PATCH] Make `sudo` calls cross platform --- scripts/Common/Scripts/Operations.ps1 | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index 911e48bd..0e2e5efe 100644 --- a/scripts/Common/Scripts/Operations.ps1 +++ b/scripts/Common/Scripts/Operations.ps1 @@ -68,7 +68,6 @@ $null = New-Module { if ($IsWindows) { $env:WSLENV = "CONFIG_NAME"; - New-Alias -Force "sudo" gsudo; Backup-PowerScheme; } @@ -164,6 +163,33 @@ $null = New-Module { continue; } + function global:Invoke-Sudo { + param( + $u, + [Parameter(ValueFromRemainingArguments, Position = 0)] + [string[]] $ArgumentList + ) + + $params = @(); + + if ($u) { + $params = @("-u", $u); + } + elseif ($ArgumentList[0] -eq "-u") { + $user = $ArgumentList[1]; + $ArgumentList = $ArgumentList | Select-Object -Skip 2; + $params = @("-u", $user); + } + + gsudo @params { + $command = $args[0]; + $flags = ($args | Select-Object -Skip 1); + & $command @flags; + } -args $ArgumentList + } + + New-Alias -Force "sudo" -Scope Global Invoke-Sudo; + if ($env:DEBUG) { & { $sys32 = "$env:WINDIR/System32";