diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cac080..7339950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,3 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed - Removed SideBySide option as per [#61](https://github.com/chocolatey-community/Chocolatey/issues/61). + +### Fixed + +- Fixed [#68](https://github.com/chocolatey-community/Chocolatey/issues/68) by making sure it's set to the + correct Path. diff --git a/source/Classes/002.ChocolateyPackage.ps1 b/source/Classes/002.ChocolateyPackage.ps1 index 207ae00..f10efd5 100644 --- a/source/Classes/002.ChocolateyPackage.ps1 +++ b/source/Classes/002.ChocolateyPackage.ps1 @@ -73,7 +73,19 @@ class ChocolateyPackage return $currentState } - $localPackage = Get-ChocolateyPackage -LocalOnly -Name $this.Name -Exact + try + { + $localPackage = Get-ChocolateyPackage -LocalOnly -Name $this.Name -Exact + } + catch + { + Write-Verbose -Message ('Exception Caught:' -f $_) + $localPackage = $null + $currentState.Reasons += @{ + code = 'ChocolateyPackage:ChocolateyPackage:ChocolateyError' + phrase = ('Error: {0}.' -f $_) + } + } if ($null -eq $localPackage) { @@ -195,7 +207,7 @@ class ChocolateyPackage { $currentState = $this.Get() - if ($currentState.Reasons.Code.Where({$_ -in @('BelowExpectedVersion','ShouldBeInstalled','ShouldNotBeInstalled','')})) + if ($currentState.Reasons.Code.Where({$_ -in @('BelowExpectedVersion','ShouldBeInstalled','ShouldNotBeInstalled')})) { return $false } @@ -208,7 +220,7 @@ class ChocolateyPackage [void] Set() { [ChocolateyPackage] $currentState = $this.Get() - $chocoCommand = Get-Command 'Install-ChocolateyPackage' + $chocoCommand = Get-Command -Name 'Install-ChocolateyPackage' [hashtable] $chocoCommandParams = @{ Name = $this.Name Confirm = $false diff --git a/source/Classes/003.ChocolateySoftware.ps1 b/source/Classes/003.ChocolateySoftware.ps1 index 8451757..6823c0d 100644 --- a/source/Classes/003.ChocolateySoftware.ps1 +++ b/source/Classes/003.ChocolateySoftware.ps1 @@ -76,6 +76,7 @@ class ChocolateySoftware if ($null -eq $chocoExe) { + Write-Verbose -Message ('Path: {0}' -f $ENV:Path) $currentState.Ensure = 'Absent' } else diff --git a/source/Classes/004.ChocolateySource.ps1 b/source/Classes/004.ChocolateySource.ps1 index 6369005..a927488 100644 --- a/source/Classes/004.ChocolateySource.ps1 +++ b/source/Classes/004.ChocolateySource.ps1 @@ -96,7 +96,20 @@ class ChocolateySource $currentState = [ChocolateySource]::New() $currentState.Name = $this.Name - $localSource = Get-ChocolateySource -Name $this.Name -ErrorAction 'Ignore' + try + { + $localSource = Get-ChocolateySource -Name $this.Name + } + catch + { + Write-Verbose -Message ('Exception Caught:' -f $_) + $localSource = $null + $currentState.Reasons += @{ + code = 'ChocolateySource:ChocolateySource:ChocolateyError' + phrase = ('Error: {0}.' -f $_) + } + } + if ($localSource.Name -eq $this.Name) { $currentState.Ensure = 'Present' diff --git a/source/WikiSource/Home.md b/source/WikiSource/Home.md index 1793b66..3f5b8b0 100644 --- a/source/WikiSource/Home.md +++ b/source/WikiSource/Home.md @@ -1,3 +1,4 @@ # Chocolatey PowerShell module +*chocolatey v#.#.#* Welcome to the Chocolatey PowerShell Module wiki! diff --git a/source/private/Get-ChocolateyInstallPath.ps1 b/source/private/Get-ChocolateyInstallPath.ps1 new file mode 100644 index 0000000..cf0303f --- /dev/null +++ b/source/private/Get-ChocolateyInstallPath.ps1 @@ -0,0 +1,41 @@ + +<# +.SYNOPSIS +Gets environment variable ChocolateyInstall from Machine scope. + +.DESCRIPTION +This command gets the machine-scoped environment variable 'ChocolateyInstall', +and make sure it's set if the folder is present but variable is not. +If the variable is not set and the choclatey folder can't be found, +the command will write to the error stream. + +.EXAMPLE +Get-ChocolateyInstallPath -ErrorAction 'Stop' + +#> +function Get-ChocolateyInstallPath +{ + [CmdletBinding()] + [OutputType([string])] + param + ( + # + ) + + $chocoInstallPath = [Environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine') + if ([string]::IsNullOrEmpty($chocoPath) -and (Test-Path -Path (Join-Path -Path $env:ProgramData -ChildPath 'Chocolatey'))) + { + $chocoInstallPath = Join-Path -Path $env:ProgramData -ChildPath 'Chocolatey' + [Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoInstallPath, 'Machine') + } + elseif (-not [string]::IsNullOrEmpty($chocoInstallPath)) + { + Write-Debug -Message ('ChocolateyInstall path Machine environmen variable already set to ''{0}''.' -f $chocoInstallPath) + } + else + { + Write-Error -Message 'The chocolatey install Machine environment variable couldn''t be found.' + } + + return $chocoInstallPath +} diff --git a/source/public/Get-ChocolateyPackage.ps1 b/source/public/Get-ChocolateyPackage.ps1 index 2b837cc..1994d19 100644 --- a/source/public/Get-ChocolateyPackage.ps1 +++ b/source/public/Get-ChocolateyPackage.ps1 @@ -161,8 +161,14 @@ function Get-ChocolateyPackage (($Name -and $Exact) -or ([string]::IsNullOrEmpty($Name))) ) { - $CacheFolder = Join-Path -Path $Env:ChocolateyInstall -ChildPath 'cache' - $CachePath = Join-Path -Path $CacheFolder -ChildPath 'GetChocolateyPackageCache.xml' + $chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'SilentlyContinue' + if ([string]::IsNullOrEmpty($chocoInstallPath)) + { + Write-Verbose -Message 'Chocolatey Software is not installed on this system.' + return + } + + $cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml') try { if (-not (Test-Path -Path $CacheFolder)) diff --git a/source/public/Install-ChocolateyPackage.ps1 b/source/public/Install-ChocolateyPackage.ps1 index a913c37..e8eb057 100644 --- a/source/public/Install-ChocolateyPackage.ps1 +++ b/source/public/Install-ChocolateyPackage.ps1 @@ -302,11 +302,12 @@ function Install-ChocolateyPackage throw "Chocolatey Software not found." } - $CachePath = [io.path]::Combine($Env:ChocolateyInstall, 'cache', 'GetChocolateyPackageCache.xml') - if ( (Test-Path -Path $CachePath)) + $chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'Stop' + $cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml') + if ((Test-Path -Path $cachePath)) { Write-Debug -Message 'Removing cache begin of Install-ChocolateyPackage' - $null = Remove-Item -Path $CachePath -ErrorAction SilentlyContinue -Force -Confirm:$false + $null = Remove-Item -Path $cachePath -ErrorAction SilentlyContinue -Force -Confirm:$false Write-Debug -Message 'Removed' } } diff --git a/source/public/Install-ChocolateySoftware.ps1 b/source/public/Install-ChocolateySoftware.ps1 index b6ff90e..5a08222 100644 --- a/source/public/Install-ChocolateySoftware.ps1 +++ b/source/public/Install-ChocolateySoftware.ps1 @@ -75,19 +75,14 @@ function Install-ChocolateySoftware { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] - [CmdletBinding( - DefaultParameterSetName = 'FromFeedUrl' - )] - param ( - [Parameter( - ParameterSetName = 'FromPackageUrl' - )] + [CmdletBinding(DefaultParameterSetName = 'FromFeedUrl')] + param + ( + [Parameter(ParameterSetName = 'FromPackageUrl')] [uri] $ChocolateyPackageUrl, - [Parameter( - ParameterSetName = 'FromFeedUrl' - )] + [Parameter(ParameterSetName = 'FromFeedUrl')] [uri] $PackageFeedUrl = 'https://chocolatey.org/api/v2', @@ -139,21 +134,21 @@ function Install-ChocolateySoftware } catch { - Write-Warning 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to do one or more of the following: (1) upgrade to .NET Framework 4.5+ and PowerShell v3, (2) specify internal Chocolatey package location (set $env:chocolateyDownloadUrl prior to install or host the package internally), (3) use the Download + PowerShell method of install. See https://chocolatey.org/install for all install options.' + Write-Warning -Message 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to do one or more of the following: (1) upgrade to .NET Framework 4.5+ and PowerShell v3, (2) specify internal Chocolatey package location (set $env:chocolateyDownloadUrl prior to install or host the package internally), (3) use the Download + PowerShell method of install. See https://chocolatey.org/install for all install options.' } switch ($PSCmdlet.ParameterSetName) { 'FromFeedUrl' { - if ($PackageFeedUrl -and ![string]::IsNullOrEmpty($Version)) + if ($PackageFeedUrl -and -not [string]::IsNullOrEmpty($Version)) { - Write-Verbose "Downloading specific version of Chocolatey: $Version" + Write-Verbose -Message "Downloading specific version of Chocolatey: $Version" $url = "$PackageFeedUrl/package/chocolatey/$Version" } else { - if (![string]::IsNullOrEmpty($PackageFeedUrl)) + if (-not [string]::IsNullOrEmpty($PackageFeedUrl)) { $url = $PackageFeedUrl } @@ -203,13 +198,14 @@ function Install-ChocolateySoftware $null = New-Item -path $tempDir -ItemType Directory } - $file = Join-Path $tempDir "chocolatey.zip" + $file = Join-Path -Path $tempDir -ChildPath "chocolatey.zip" # Download the Chocolatey package Write-Verbose -Message "Getting Chocolatey from $url." $GetRemoteFileParams = @{ url = $url file = $file } + $GetRemoteFileParamsName = (get-command Get-RemoteFile).parameters.keys $KeysForRemoteFile = $PSBoundParameters.keys | Where-Object { $_ -in $GetRemoteFileParamsName } foreach ($key in $KeysForRemoteFile ) @@ -217,6 +213,7 @@ function Install-ChocolateySoftware Write-Debug "`tWith $key :: $($PSBoundParameters[$key])" $null = $GetRemoteFileParams.Add($key , $PSBoundParameters[$key]) } + $null = Get-RemoteFile @GetRemoteFileParams # unzip the package @@ -242,42 +239,53 @@ function Install-ChocolateySoftware } # Call chocolatey install - Write-Verbose "Installing chocolatey on this machine." + Write-Verbose -Message "Installing chocolatey on this machine." $TempTools = [io.path]::combine($tempDir, 'tools') # To be able to mock - $chocInstallPS1 = Join-Path $TempTools 'chocolateyInstall.ps1' + $chocInstallPS1 = Join-Path -Path $TempTools -ChildPath 'chocolateyInstall.ps1' if ($InstallationDirectory) { [Environment]::SetEnvironmentVariable('ChocolateyInstall', $InstallationDirectory, 'Machine') - [Environment]::SetEnvironmentVariable('ChocolateyInstall', $InstallationDirectory, 'Process') } + & $chocInstallPS1 | Write-Debug + Write-Verbose -Message 'Ensuring chocolatey commands are on the path.' + [string] $chocoPath = '' - Write-Verbose 'Ensuring chocolatey commands are on the path.' - $chocoPath = [Environment]::GetEnvironmentVariable('ChocolateyInstall') - if ([string]::IsNullOrEmpty($chocoPath)) + if ($chocoPath = [Environment]::GetEnvironmentVariable('ChocolateyInstall','Machine')) { - $chocoPath = "$env:ALLUSERSPROFILE\Chocolatey" + Write-Debug -Message ('The Machine Environment variable is already set to: ''{0}''.' -f $chocoPath) } - - if (-not (Test-Path -Path $chocoPath)) + else { - $chocoPath = "$env:SYSTEMDRIVE\ProgramData\Chocolatey" + # Checking if it was installed in AllUserProfile/Chocolatey (was default many years ago) + $chocoPath = Join-Path -Path $env:ALLUSERSPROFILE -ChildPath 'Chocolatey' + if (-not (Test-Path -Path $chocoPath)) + { + # The AllUserProfile/Chocolatey folder does not exit, let's install in correct default location + $chocoPath = Join-Path -Path $env:ProgramData -ChildPath 'Chocolatey' + } + + # Set the Machine-scoped 'ChocolateyInstall' environement variable + Write-Debug -Message ('Setting the Machine & Process Environment variable to: ''{0}''.' -f $chocoPath) + [Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoPath, 'Machine') + [Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoPath, 'Process') } $chocoExePath = Join-Path -Path $chocoPath -ChildPath 'bin' - if ($($env:Path).ToLower().Contains($($chocoExePath).ToLower()) -eq $false) + if (@($env:Path.ToLower() -split [io.path]::PathSeparator) -notcontains $chocoExePath.ToLower()) { - $env:Path = [Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + # we can't see the choco bin folder in $env:Path, trying to load from Machine. + $env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') } - Write-Verbose 'Ensuring chocolatey.nupkg is in the lib folder' + Write-Verbose -Message 'Ensuring chocolatey.nupkg is in the lib folder' $chocoPkgDir = Join-Path -Path $chocoPath -ChildPath 'lib\chocolatey' $nupkg = Join-Path -Path $chocoPkgDir -ChildPath 'chocolatey.nupkg' $null = [System.IO.Directory]::CreateDirectory($chocoPkgDir) - Copy-Item -Path "$file" -Destination "$nupkg" -Force -ErrorAction SilentlyContinue + Copy-Item -Path $file -Destination $nupkg -Force -ErrorAction 'SilentlyContinue' if ($ChocoVersion = & "$chocoPath\choco.exe" @('-v')) { diff --git a/source/public/Test-ChocolateyInstall.ps1 b/source/public/Test-ChocolateyInstall.ps1 index 1adba22..93e8c2e 100644 --- a/source/public/Test-ChocolateyInstall.ps1 +++ b/source/public/Test-ChocolateyInstall.ps1 @@ -40,7 +40,7 @@ function Test-ChocolateyInstall if (-not ($chocoCmd = @(Get-Command 'choco.exe' -CommandType 'Application' -ErrorAction 'SilentlyContinue')[0])) { if ( - !$InstallDir -or + -not $InstallDir -or $chocoCmd.Path -match [regex]::Escape($InstallDir) ) { @@ -57,7 +57,7 @@ function Test-ChocolateyInstall } else { - Write-Verbose "Chocolatey Software not found." + Write-Verbose -Message 'Chocolatey Software not found.' return $false } } diff --git a/source/public/Test-ChocolateySource.ps1 b/source/public/Test-ChocolateySource.ps1 index bf09669..258eeda 100644 --- a/source/public/Test-ChocolateySource.ps1 +++ b/source/public/Test-ChocolateySource.ps1 @@ -107,7 +107,7 @@ function Test-ChocolateySource { if (-not (Get-Command 'choco.exe' -CommandType Application -ErrorAction SilentlyContinue)) { - throw "Chocolatey Software not found." + throw "Chocolatey Software not found. 17" } if (-not ($Source = (Get-ChocolateySource -Name $Name)) ) diff --git a/source/public/Uninstall-Chocolatey.ps1 b/source/public/Uninstall-Chocolatey.ps1 index 134e39a..534c781 100644 --- a/source/public/Uninstall-Chocolatey.ps1 +++ b/source/public/Uninstall-Chocolatey.ps1 @@ -32,7 +32,7 @@ function Uninstall-Chocolatey [Parameter()] [AllowNull()] [System.String] - $InstallDir = $Env:ChocolateyInstall, + $InstallDir = $(Get-ChocolateyInstallPath -ErrorAction 'SilentlyContinue'), [Parameter(DontShow)] [switch] @@ -41,7 +41,7 @@ function Uninstall-Chocolatey process { - #If InstallDir is empty or null, select from whee choco.exe is available + #If InstallDir is empty or null, select from the choco.exe if available if (-not $InstallDir) { Write-Debug -Message "Attempting to find the choco.exe command." @@ -59,7 +59,7 @@ function Uninstall-Chocolatey } } - Write-Verbose "Chocolatey Installation Folder is $InstallDir" + Write-Verbose -Message "Chocolatey Installation Folder is $InstallDir" $chocoFiles = @('choco.exe', 'chocolatey.exe', 'cinst.exe', 'cuninst.exe', 'clist.exe', 'cpack.exe', 'cpush.exe', 'cver.exe', 'cup.exe').Foreach{ $_; "$_.old" } #ensure the .old are also removed @@ -96,7 +96,7 @@ function Uninstall-Chocolatey Write-Verbose -Message "Removing $InstallDir from the Path and the ChocolateyInstall Environment variable." [Environment]::SetEnvironmentVariable('ChocolateyInstall', $null, 'Machine') - $Env:ChocolateyInstall = $null + [Environment]::SetEnvironmentVariable('ChocolateyInstall', $null, 'Process') $AllPaths = [Environment]::GetEnvironmentVariable('Path', 'machine').split(';').where{ ![string]::IsNullOrEmpty($_) -and $_ -notmatch "^$([regex]::Escape($InstallDir))\\bin$" diff --git a/source/public/Uninstall-ChocolateyPackage.ps1 b/source/public/Uninstall-ChocolateyPackage.ps1 index 0260e76..e8e34b3 100644 --- a/source/public/Uninstall-ChocolateyPackage.ps1 +++ b/source/public/Uninstall-ChocolateyPackage.ps1 @@ -279,12 +279,15 @@ function Uninstall-ChocolateyPackage { throw "Chocolatey Software not found." } - $CachePath = [io.path]::Combine($Env:ChocolateyInstall, 'cache', 'GetChocolateyPackageCache.xml') - if ( (Test-Path $CachePath)) + + $chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'Stop' + $cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml') + if ( (Test-Path $cachePath)) { - $null = Remove-Item -Path $CachePath -ErrorAction 'SilentlyContinue' -Confirm:$false + $null = Remove-Item -Path $cachePath -ErrorAction 'SilentlyContinue' -Confirm:$false } } + process { foreach ($PackageName in $Name) diff --git a/source/public/Update-ChocolateyPackage.ps1 b/source/public/Update-ChocolateyPackage.ps1 index 6d4a7d1..6172407 100644 --- a/source/public/Update-ChocolateyPackage.ps1 +++ b/source/public/Update-ChocolateyPackage.ps1 @@ -341,10 +341,12 @@ function Update-ChocolateyPackage { throw "Chocolatey Software not found." } - $CachePath = [io.path]::Combine($Env:ChocolateyInstall, 'cache', 'GetChocolateyPackageCache.xml') - if ( (Test-Path $CachePath)) + + $chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'Stop' + $cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml') + if ( (Test-Path $cachePath)) { - $null = Remove-Item -Path $CachePath -ErrorAction 'SilentlyContinue' -Confirm:$false + $null = Remove-Item -Path $cachePath -ErrorAction 'SilentlyContinue' -Confirm:$false } } diff --git a/tests/Unit/Private/Get-ChocolateyInstallPath.tests.ps1 b/tests/Unit/Private/Get-ChocolateyInstallPath.tests.ps1 new file mode 100644 index 0000000..943c46a --- /dev/null +++ b/tests/Unit/Private/Get-ChocolateyInstallPath.tests.ps1 @@ -0,0 +1 @@ +# Not yet tested diff --git a/tests/kitchen/GCPackages/ChocolateyIsInstalled/ChocolateyIsInstalled.tests.ps1 b/tests/kitchen/GCPackages/ChocolateyIsInstalled/ChocolateyIsInstalled.tests.ps1 index 60799e7..146e527 100644 --- a/tests/kitchen/GCPackages/ChocolateyIsInstalled/ChocolateyIsInstalled.tests.ps1 +++ b/tests/kitchen/GCPackages/ChocolateyIsInstalled/ChocolateyIsInstalled.tests.ps1 @@ -20,9 +20,9 @@ Describe 'Test ChocolateyIsInstalled Package' { } it 'Gets the ChocolateyIsInstalled Package Compliance Status (non-compliant)' { - + Uninstall-Chocolatey -ErrorAction 'Ignore' $result = $null - $result = Get-GuestConfigurationPackageComplianceStatus -Path $packageZip + $result = Get-GuestConfigurationPackageComplianceStatus -Path $packageZip -Verbose $result.Resources.Reasons | Should -not -BeNullOrEmpty $result.complianceStatus | Should -be $false }