Skip to content

Commit

Permalink
fix(sudo): support pwsh (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen authored and Chawye Hsu committed Jan 26, 2020
1 parent de1a069 commit c7116ac
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions sudo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Set-StrictMode -Off;

if(!$args) { "usage: sudo <cmd...>"; exit 1 }

$powershellExe = Get-Process -Id $pid | Select-Object -ExpandProperty Path
$commandPrefix = ''
if ($host.Version.Major -gt 5) {
$commandPrefix = '-Command'
}

function is_admin {
return ([System.Security.Principal.WindowsIdentity]::GetCurrent().UserClaims | ? { $_.Value -eq 'S-1-5-32-544'})
}
Expand All @@ -20,10 +26,10 @@ function sudo_do($parent_pid, $dir, $cmd) {

$kernel::freeconsole()
$kernel::attachconsole($parent_pid)

$p = new-object diagnostics.process; $start = $p.startinfo
$start.filename = "powershell.exe"
$start.arguments = "-noprofile $cmd`nexit `$lastexitcode"
$start.filename = "$powershellExe"
$start.arguments = "-noprofile $commandPrefix $cmd`nexit `$lastexitcode"
$start.useshellexecute = $false
$start.workingdirectory = $dir
$p.start()
Expand Down Expand Up @@ -52,7 +58,7 @@ if(!(is_admin)) {
}

$a = if ($args[0] -eq '-please' -or $args[0] -eq '-plz') {
Get-History -Count 1 | select -ExpandProperty CommandLine
Get-History -Count 1 | Select-Object -ExpandProperty CommandLine
} else {
serialize $args $true
}
Expand All @@ -61,9 +67,10 @@ $wd = serialize (convert-path $pwd) # convert-path in case pwd is a PSDrive

$savetitle = $host.ui.rawui.windowtitle
$p = new-object diagnostics.process; $start = $p.startinfo
$start.filename = "powershell.exe"
$start.arguments = "-noprofile & '$pscommandpath' -do $wd $pid $a`nexit `$lastexitcode"
$start.filename = "$powershellExe"
$start.arguments = "-noprofile $commandPrefix & '$pscommandpath' -do $wd $pid $a`nexit `$lastexitcode"
$start.verb = 'runas'
$start.UseShellExecute = $true
$start.windowstyle = 'hidden'
try { $null = $p.start() }
catch { exit 1 } # user didn't provide consent
Expand Down

0 comments on commit c7116ac

Please sign in to comment.