diff --git a/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 b/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 index 3a2079a6fa..c4e328d79b 100644 --- a/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 @@ -10,11 +10,67 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. +# limitations under the License. + +function Get-EnvironmentVariable { +<# +.SYNOPSIS +Gets an Environment Variable. -function Get-EnvironmentVariable([string] $Name, [System.EnvironmentVariableTarget] $Scope) { - [Environment]::GetEnvironmentVariable($Name, $Scope) -} +.DESCRIPTION +This will will get an environment variable based on the variable name and scope while accounting whether to expand the variable or not (e.g.: %TEMP% -> C:\User\Username\AppData\Local\Temp). -# Some enhancements to think about here. -# $machinePath = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment\").GetValue("PATH", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames).ToString(); +.PARAMETER Name +The environemnt variable you want to get the value from. + +.PARAMETER Scope +The environemnt variable target scope. + +.PARAMETER PreserveVariables +A switch parameter stating whether you want to expand the variables or not. Defaults to false. + +.EXAMPLE +Get-EnvironmentVariable 'TEMP' User -PreserveVariables + +.NOTES +This helper reduces the number of lines one would have to write to get environment variables, mainly when not expanding the variables is a must. +#> +[CmdletBinding()] +[OutputType([string])] +param( + [Parameter(Mandatory=$true)] + [string] $Name, + [Parameter(Mandatory=$true)] + [System.EnvironmentVariableTarget] $Scope, + [Parameter(Mandatory=$false)] + [switch] $PreserveVariables = $false +) + [string] $MACHINE_ENVIRONMENT_REGISTRY_KEY_NAME = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment\"; + [Microsoft.Win32.RegistryKey] $win32RegistryKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($MACHINE_ENVIRONMENT_REGISTRY_KEY_NAME) + if ($Scope -eq [System.EnvironmentVariableTarget]::User) { + [string] $USER_ENVIRONMENT_REGISTRY_KEY_NAME = "Environment"; + [Microsoft.Win32.RegistryKey] $win32RegistryKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($USER_ENVIRONMENT_REGISTRY_KEY_NAME) + } elseif ($Scope -eq [System.EnvironmentVariableTarget]::Process) { + [Environment]::GetEnvironmentVariable($Name, $Scope) + } + + [Microsoft.Win32.RegistryValueOptions] $registryValueOptions = [Microsoft.Win32.RegistryValueOptions]::None + + if ($PreserveVariables) { + Write-Verbose "Choosing not to expand environment names" + $registryValueOptions = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames + } + + [string] $environmentVariableValue = [string]::Empty + + try { + Write-Verbose "Getting environment variable $Name" + $environmentVariableValue = $win32RegistryKey.GetValue($Name, [string]::Empty, $registryValueOptions) + } catch { + Write-Debug "Unable to retrieve the $Name environment variable. Details: $_" + } finally { + $win32RegistryKey.Close() + } + + return $environmentVariableValue +} \ No newline at end of file diff --git a/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 b/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 index 5e0f4370ec..2f6153c17a 100644 --- a/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 +++ b/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 @@ -10,8 +10,8 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. - +# limitations under the License. + function Install-ChocolateyPath { param( [string] $pathToInstall, @@ -26,7 +26,7 @@ param( if (!$envPath.ToLower().Contains($pathToInstall.ToLower())) { Write-Host "PATH environment variable does not have $pathToInstall in it. Adding..." - $actualPath = Get-EnvironmentVariable -Name 'Path' -Scope $pathType + $actualPath = Get-EnvironmentVariable -Name 'Path' -Scope $pathType -PreserveVariables $statementTerminator = ";" #does the path end in ';'?