Skip to content

Commit

Permalink
Merge branch 'pr600-path-cont' into stable
Browse files Browse the repository at this point in the history
* pr600-path-cont:
  (GH-303) Get-EnvironmentVariable Changes
  (GH-303) Install-ChocolateyPath - Do Not Expand Variables in PATH
  • Loading branch information
ferventcoder committed Apr 9, 2016
2 parents 3d67a72 + 9bbfb4e commit e050c22
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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

$statementTerminator = ";"
#does the path end in ';'?
Expand Down

0 comments on commit e050c22

Please sign in to comment.