Skip to content

Commit

Permalink
(chocolatey#2044) Fix for changing $env:Temp
Browse files Browse the repository at this point in the history
Running as `SYSTEM`, the `$env:Temp` variable is normally
*C:\Windows\Temp*. If the **first** package to ever use
`Update-SessionEnvironment.ps1` happens to be running as `SYSTEM`, the
location of `$env:Temp` changes to
*C:\Windows\system32\config\systemprofile\AppData\Local\Temp* for the
duration of the session.  This change to `$env:Temp` does not occur in
subsequent calls to `Update-SessionEnvironment.ps1`.  It is unknown why
this change occurs at all and likewise is unknown why it occurs for only
the first time.

The `Update-SessionEnvironment.ps1` normally gives priority to
user-scope environment variables over machine-scope environment
variables.  Changes in this commit eliminates the gathering of the
`User` scope environment variables when running as the `SYSTEM` account
on the belief that reason to be operating under the `SYSTEM` account is
to do machine-wide activities, and there is no reason to see `SYSTEM`
as a "user".  On a clean system, the only User-scope environment
variables under `SYSTEM` are *path*, *temp*, and *tmp* which are already
defined under the Machine-scope, and in general, there shouldn't be any
new User-scope variables for the SYSTEM account because it's not a
"normal" account.  Basically, there should be little reason to expect
this change to break anything.
  • Loading branch information
gep13 committed May 5, 2021
1 parent aaf3d6c commit c408d12
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,18 @@ None
$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...
$ScopeList = 'Process', 'Machine'
if ($userName -notin 'SYSTEM', "${env:COMPUTERNAME}`$") {
# but only if not running as the SYSTEM/machine in which case user can be ignored.
$ScopeList += 'User'
}
foreach ($Scope in $ScopeList) {
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' |
Expand Down

0 comments on commit c408d12

Please sign in to comment.