Encontrei as informações a seguir na documentação get-help -full start-process
:
# Starts a PowerShell process with "Run as Administrator" permissions.
PS C:>start-process powershell.exe -verb runas
O melhor que posso dizer é que runas.exe
pode alterar seu usuário, mas não elevará suas permissões. No entanto, com base na citação acima, o Start-Process
process do powershell pode ser usado para fazer isso, então acabei fazendo o seguinte na minha função sudo
, que parece estar funcionando bem:
$MY_PROFILE_TMP="$Env:TMP\my_profile_tmp";
Function Init-TMP() {
#Make sure my personal tmp directory exists
if (!(Test-Path $MY_PROFILE_TMP)) {
mkdir $MY_PROFILE_TMP | Out-Null
}
}
Function As-Admin() {
Init-TMP
#Create unique temp filenames to capture stderr and stdout
$GUID=New-Guid
$ADMIN_OUT="$MY_PROFILE_TMP\$($GUID)_OUT.txt"
$ADMIN_ERR="$MY_PROFILE_TMP\$($GUID)_ERR.txt"
#Remove temp files if for some anomoly or error they already exist
rm -ErrorAction Ignore $ADMIN_OUT
rm -ErrorAction Ignore $ADMIN_ERR
#Start powershell with elevated permissions and write to the tmp out and error files
#Without the &{}, invalid commands like 'sudo dinosaur' don't get
#captured to the error file
Start-Process powershell "& { $args } 2>$ADMIN_ERR > $ADMIN_OUT" -Verb runas -Wait -WindowStyle Hidden
#Write to the current console the out and error results from the elevated process
#TODO ideally, we'd read from these as a stream so the order would be correct...
cat $ADMIN_ERR -Delimiter None | Write-Error
cat $ADMIN_OUT
#Remove the temp files
rm -ErrorAction Ignore $ADMIN_OUT
rm -ErrorAction Ignore $ADMIN_ERR
}
#Make an alias "sudo"
New-Alias -Name sudo -Value As-Admin