Skip to content

Commit

Permalink
(GH-2051) Fix: workingDirectory always replaced w/cacheLocation
Browse files Browse the repository at this point in the history
The previous commit 9e2e49a 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.
  • Loading branch information
Ian Freislich authored and ferventcoder committed May 16, 2020
1 parent 7298fd8 commit 02ea65e
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,22 @@ 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
)
[string]$statements = $statements -join ' '

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) {

This comment has been minimized.

Copy link
@ferventcoder

ferventcoder Mar 10, 2021

Member

This should probably be:

if (!$workingDirectory) {

$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
Expand Down

1 comment on commit 02ea65e

@ferventcoder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow on at #2208

Please sign in to comment.