From 02ea65ee8e10a1719391a15577f7bde57a06beac Mon Sep 17 00:00:00 2001 From: Ian Freislich Date: Mon, 24 Feb 2020 12:42:29 -0500 Subject: [PATCH] (GH-2051) Fix: workingDirectory always replaced w/cacheLocation The previous commit 9e2e49a68 to address UNC paths broke the ability to provide a working directory to the Start-ChocolateyProcessAsAdmin helper function. The test `$workingDirectory.ProviderPath -eq $null` always evaluates to true because the String type does not have the member ProviderPath. The fix is to obtain the location object only when no path is provided to the helper and to verify its validity before use, possibly falling back to the cache location, similar to how it was implemented for Get-ChocolateyUnzip. --- .../functions/Start-ChocolateyProcessAsAdmin.ps1 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 index 875ba71a5a..8ffa47f731 100644 --- a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 +++ b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 @@ -118,7 +118,7 @@ param( [parameter(Mandatory=$false)][switch] $minimized, [parameter(Mandatory=$false)][switch] $noSleep, [parameter(Mandatory=$false)] $validExitCodes = @(0), - [parameter(Mandatory=$false)][string] $workingDirectory = $(Get-Location -PSProvider 'FileSystem'), + [parameter(Mandatory=$false)][string] $workingDirectory = $null, [parameter(Mandatory=$false)][string] $sensitiveStatements = '', [parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments ) @@ -126,12 +126,14 @@ param( Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters - if ($workingDirectory -eq $null -or $workingDirectory.ProviderPath -eq $null) { - Write-Debug "Unable to use current location for Working Directory. Using Cache Location instead." - $workingDirectory = $env:TEMP + if ($workingDirectory -eq $null) { + $pwd = $(Get-Location -PSProvider 'FileSystem') + if ($pwd -eq $null -or $pwd.ProviderPath -eq $null) { + Write-Debug "Unable to use current location for Working Directory. Using Cache Location instead." + $workingDirectory = $env:TEMP + } + $workingDirectory = $pwd.ProviderPath } - $workingDirectory = $workingDirectory.ProviderPath - $alreadyElevated = $false if (Test-ProcessAdminRights) { $alreadyElevated = $true