Skip to content

Commit

Permalink
(GH-375) HKCU may not have Environment
Browse files Browse the repository at this point in the history
When attempting to expand or work with `HKCU:\Environment`, that can
error due to not having an existing `Environment` key in HKCU. This can
occur in certain Windows Server versions, such as Server Core which
doesn't have the key. This can also happen in other situations as well.

- In `Get-EnvironmentVariableNames`, when expanding from
   `HKCU:\Environment` and it fails, silently continue.
- When attempting to remove variables at User scope for
   Get-ToolsLocation, fail those silently.
- If you can't set the Get-ToolsLocation value for User scope, attempt
   Machine scope instead.
- With `Install-ChocolateyEnvironmentVariable`, if the attempt to set
   to User scope fails and the user is an administrator, attempt to set
   it with Machine scope. Otherwise fail.
- In `Get-EnvironmentVariable`, if the registry key is null, do not
   attempt to grab a value or close it.
  • Loading branch information
ferventcoder committed Aug 3, 2016
1 parent 66b0107 commit b041d2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ param(

try {
#Write-Verbose "Getting environment variable $Name"
$environmentVariableValue = $win32RegistryKey.GetValue($Name, [string]::Empty, $registryValueOptions)
if ($win32RegistryKey -ne $null) {
# Some versions of Windows do not have HKCU:\Environment
$environmentVariableValue = $win32RegistryKey.GetValue($Name, [string]::Empty, $registryValueOptions)
}
} catch {
Write-Debug "Unable to retrieve the $Name environment variable. Details: $_"
} finally {
$win32RegistryKey.Close()
if ($win32RegistryKey -ne $null) {
$win32RegistryKey.Close()
}
}

if ($environmentVariableValue -eq $null -or $environmentVariableValue -eq '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Get-EnvironmentVariable
Set-EnvironmentVariable
#>

# HKCU:\Environment may not exist in all Windows OSes (such as Server Core).
switch ($Scope) {
'User' { Get-Item 'HKCU:\Environment' | Select-Object -ExpandProperty Property }
'User' { Get-Item 'HKCU:\Environment' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Property }
'Machine' { Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | Select-Object -ExpandProperty Property }
'Process' { Get-ChildItem Env:\ | Select-Object -ExpandProperty Key }
default { throw "Unsupported environment scope: $Scope" }
Expand Down
17 changes: 13 additions & 4 deletions src/chocolatey.resources/helpers/functions/Get-ToolsLocation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,30 @@ None
if ($binRoot -eq $null) {
$binRoot = $olderRoot
}
Set-EnvironmentVariable -Name "chocolatey_bin_root" -Value '' -Scope User
Set-EnvironmentVariable -Name "chocolatey_bin_root" -Value '' -Scope User -ErrorAction SilentlyContinue
}

$toolsLocation = $binRoot
Set-EnvironmentVariable -Name "ChocolateyBinRoot" -Value '' -Scope User
Set-EnvironmentVariable -Name "ChocolateyBinRoot" -Value '' -Scope User -ErrorAction SilentlyContinue
}
}

# Add a drive letter if one doesn't exist
if (-not($toolsLocation -imatch "^\w:")) {
$toolsLocation = join-path $env:systemdrive $toolsLocation
$toolsLocation = Join-Path $env:systemdrive $toolsLocation
}

if (-not($env:ChocolateyToolsLocation -eq $toolsLocation)) {
Set-EnvironmentVariable -Name "ChocolateyToolsLocation" -Value $toolsLocation -Scope User
try {
Set-EnvironmentVariable -Name "ChocolateyToolsLocation" -Value $toolsLocation -Scope User
} catch {
if (Test-ProcessAdminRights) {
# sometimes User scope may not exist (such as with core)
Set-EnvironmentVariable -Name "ChocolateyToolsLocation" -Value $toolsLocation -Scope Machine
} else {
throw $_.Exception
}
}
}

return $toolsLocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ param(
Start-ChocolateyProcessAsAdmin "$psArgs"
}
} else {
Set-EnvironmentVariable -Name $variableName -Value $variableValue -Scope $variableType
try {
Set-EnvironmentVariable -Name $variableName -Value $variableValue -Scope $variableType
} catch {
if (Test-ProcessAdminRights) {
# HKCU:\Environment may not exist, which happens sometimes with Server Core
Set-EnvironmentVariable -Name $variableName -Value $variableValue -Scope Machine
} else {
throw $_.Exception
}
}
}

Set-Content env:\$variableName $variableValue
Expand Down

0 comments on commit b041d2d

Please sign in to comment.