Skip to content

Commit

Permalink
Correct for running as SYSTEM
Browse files Browse the repository at this point in the history
This should fix an issue when running Chocolatey under the SYSTEM account [as described here](chocolatey#2044)
  • Loading branch information
teknowledgist committed May 7, 2020
1 parent b3a7db5 commit dc9fe3a
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,34 @@ None
}

if ($refreshEnv) {
Write-Output "Refreshing environment variables from the registry for powershell.exe. Please wait..."
Write-Output 'Refreshing environment variables from the registry for powershell.exe. Please wait...'
} else {
Write-Verbose "Refreshing environment variables from the registry."
Write-Verbose 'Refreshing environment variables from the registry.'
}

$userName = $env:USERNAME
$architecture = $env:PROCESSOR_ARCHITECTURE
$psModulePath = $env:PSModulePath

#ordering is important here, $user comes after so we can override $machine
'Process', 'Machine', 'User' |
% {
$scope = $_
Get-EnvironmentVariableNames -Scope $scope |
% {
Set-Item "Env:$($_)" -Value (Get-EnvironmentVariable -Scope $scope -Name $_)
#ordering is important here, $user should override $machine...
$Scopes = 'Process', 'Machine'
if (($userName -ne 'SYSTEM') -and ($userName -ne "$($env:COMPUTERNAME)`$")) {
# but only if not running as the SYSTEM/machine in which case user can be ignored.
$Scopes += 'User'
}
foreach ($Scope in $Scopes) {
Get-EnvironmentVariableNames -Scope $Scope |
ForEach-Object {
Set-Item "Env:$($_)" -Value (Get-EnvironmentVariable -Scope $Scope -Name $_)
}
}
}

#Path gets special treatment b/c it munges the two together
$paths = 'Machine', 'User' |
% {
ForEach-Object {
(Get-EnvironmentVariable -Name 'PATH' -Scope $_) -split ';'
} |
Select -Unique
Select-Object -Unique
$Env:PATH = $paths -join ';'

# PSModulePath is almost always updated by process, so we want to preserve it.
Expand All @@ -91,7 +94,7 @@ None
if ($architecture) { $env:PROCESSOR_ARCHITECTURE = $architecture; }

if ($refreshEnv) {
Write-Output "Finished"
Write-Output 'Finished'
}
}

Expand Down

0 comments on commit dc9fe3a

Please sign in to comment.