Skip to content

Commit

Permalink
(chocolateyGH-303) Install-ChocolateyPath - Do Not Expand Variables i…
Browse files Browse the repository at this point in the history
…n PATH

When adding a new element to PATH, Install-ChocolateyPath reads PATH
(wh ich contains expanded variables), appends the new element, and then
writes the value back to PATH. This results in PATH having its
variables overwritten with its values. Instead of reading $env:PATH to
find the current path, read it from the registry, which will retain the
PATH's original variables.
  • Loading branch information
mnadel authored and ferventcoder committed Apr 9, 2016
1 parent 3d67a72 commit c586463
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@
# 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) {
$reg = [Microsoft.Win32.Registry]::Machine.OpenSubKey("Environment", $true)
} else {
$reg = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Environment", $true)
}

if ($PreserveVariables -eq $True) {
$option = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
} else {
$option = [Microsoft.Win32.RegistryValueOptions]::None
}

$value = $reg.GetValue('Path', $null, $option)

$reg.Close()

return $value
}

# 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();
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ';'?
Expand Down

0 comments on commit c586463

Please sign in to comment.