Skip to content

Commit

Permalink
(chocolateyGH-303) Get-EnvironmentVariable Changes
Browse files Browse the repository at this point in the history
- Declare argument as `switch` instead of `bool`.
- Renamed some variables and added comments.
- Updated references to Get-EnvironmentVariable.
- Cleaned up formatting.
- Allowed getting process scoped environment variables again.
  • Loading branch information
tirolo committed Apr 9, 2016
1 parent c586463 commit 9bbfb4e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,65 @@
# See the License for the specific language governing permissions and
# 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)
}
function Get-EnvironmentVariable {
<#
.SYNOPSIS
Gets an Environment Variable.
.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).
.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)
}

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

$value = $reg.GetValue('Path', $null, $option)
if ($PreserveVariables) {
Write-Verbose "Choosing not to expand environment names"
$registryValueOptions = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
}

$reg.Close()
[string] $environmentVariableValue = [string]::Empty

return $value
}
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()
}

# 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();
return $environmentVariableValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 -PreserveVariables $True
$actualPath = Get-EnvironmentVariable -Name 'Path' -Scope $pathType -PreserveVariables

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

0 comments on commit 9bbfb4e

Please sign in to comment.