Skip to content

Commit

Permalink
(chocolateyGH-1126) Start-ChocolateyProcess no elevate when admin
Browse files Browse the repository at this point in the history
When calling Start-ChocolateyProcessAsAdmin from an already elevated
process, there is no reason to attempt to elevate the process.
  • Loading branch information
ferventcoder committed Jan 4, 2017
1 parent 111676a commit e208ca2
Showing 1 changed file with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,17 @@ param(

Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters

try{
$alreadyElevated = $false
if (Test-ProcessAdminRights) {
$alreadyElevated = $true
}

$dbMessagePrepend = "Elevating permissions and running"
if (!$elevated) {
$dbMessagePrepend = "Running"
}

try {
if ($exeToRun -ne $null) { $exeToRun = $exeToRun -replace "`0", "" }
if ($statements -ne $null) { $statements = $statements -replace "`0", "" }
} catch {
Expand All @@ -126,6 +136,24 @@ param(
if ($wrappedStatements -eq $null) { $wrappedStatements = ''}

if ($exeToRun -eq 'powershell') {
if ($alreadyElevated) {
$block = @"
try {
$statements
} catch {
throw
}
"@

& $block
$scriptSuccess = $?
if (-not $scriptSuccess) {
return 1
}

return 0
}

$exeToRun = "$($env:SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe"
$importChocolateyHelpers = "& import-module -name '$helpersPath\chocolateyInstaller.psm1' -Verbose:`$false | Out-Null;"
$block = @"
Expand All @@ -144,23 +172,18 @@ param(
}
"@
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($block))
$wrappedStatements = "-NoProfile -ExecutionPolicy bypass -EncodedCommand $encoded"
$wrappedStatements = "-NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat Text -OutputFormat Text -EncodedCommand $encoded"
$dbgMessage = @"
Elevating Permissions and running powershell block:
$dbMessagePrepend powershell block:
$block
This may take a while, depending on the statements.
"@

}
else
{
$dbgMessage = @"
Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may take a while, depending on the statements.
"@
}

if (!$elevated) {
$dbgMessage = @"
Running [`"$exeToRun`" $wrappedStatements]. This may take a while, depending on the statements.
$dbMessagePrepend [`"$exeToRun`" $wrappedStatements]. This may take a while, depending on the statements.
"@
}

Expand Down Expand Up @@ -225,7 +248,7 @@ Running [`"$exeToRun`" $wrappedStatements]. This may take a while, depending on
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WorkingDirectory = $workingDirectory

if ($elevated -and [Environment]::OSVersion.Version -ge (New-Object 'Version' 6,0)){
if ($elevated -and -not $alreadyElevated -and [Environment]::OSVersion.Version -ge (New-Object 'Version' 6,0)){
# this doesn't actually currently work - because we are not running under shell execute
Write-Debug "Setting RunAs for elevation"
$process.StartInfo.Verb = "RunAs"
Expand Down Expand Up @@ -266,7 +289,7 @@ Running [`"$exeToRun`" $wrappedStatements]. This may take a while, depending on
}
}

Write-Debug "Finishing 'Start-ChocolateyProcessAsAdmin'"
Write-Debug "Finishing '$($MyInvocation.InvocationName)'"

return $exitCode
}
Expand Down

0 comments on commit e208ca2

Please sign in to comment.