Skip to content

Commit

Permalink
chocolateyGH-303 Install-ChocolateyPath Expands Variables in PATH, Ov…
Browse files Browse the repository at this point in the history
…erwriting

Preexisting Variables

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 committed Jul 30, 2015
1 parent 13b4e08 commit 623d96f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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 All @@ -53,4 +53,4 @@ param(
}
}

# [System.Text.RegularExpressions.Regex]::Match($Path,[System.Text.RegularExpressions.Regex]::Escape('locationtoMatch') + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
# [System.Text.RegularExpressions.Regex]::Match($Path,[System.Text.RegularExpressions.Regex]::Escape('locationtoMatch') + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)

0 comments on commit 623d96f

Please sign in to comment.