diff --git a/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 b/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 index 3a2079a6fa..a38031c9aa 100644 --- a/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-EnvironmentVariable.ps1 @@ -10,11 +10,28 @@ # 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. - -function Get-EnvironmentVariable([string] $Name, [System.EnvironmentVariableTarget] $Scope) { - [Environment]::GetEnvironmentVariable($Name, $Scope) -} - -# 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(); +# limitations under the License. + +function Get-EnvironmentVariable([string] $Name, [System.EnvironmentVariableTarget] $Scope, [bool] $PreserveVariables = $False) { + if ($pathType -eq [System.EnvironmentVariableTarget]::Machine) { + $registry = [Microsoft.Win32.Registry]::LocalMachine + $registryPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment\" + } else { + $registry = [Microsoft.Win32.Registry]::CurrentUser + $registryPath = "Environment" + } + + $reg = $registry.OpenSubKey($registryPath, $True) + + if ($PreserveVariables -eq $True) { + $option = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames + } else { + $option = [Microsoft.Win32.RegistryValueOptions]::None + } + + $value = $reg.GetValue($Name, $null, $option).ToString() + + $reg.Close() + + return $value +} diff --git a/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 b/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 index 5e0f4370ec..9aa15f75db 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 $True $statementTerminator = ";" #does the path end in ';'? @@ -53,4 +53,4 @@ param( } } -# [System.Text.RegularExpressions.Regex]::Match($Path,[System.Text.RegularExpressions.Regex]::Escape('locationtoMatch') + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) \ No newline at end of file +# [System.Text.RegularExpressions.Regex]::Match($Path,[System.Text.RegularExpressions.Regex]::Escape('locationtoMatch') + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)