diff options
| author | eao197 <eao197@gmail.com> | 2018-05-30 19:25:16 +0300 |
|---|---|---|
| committer | eao197 <eao197@gmail.com> | 2018-05-30 19:25:16 +0300 |
| commit | 99eb78cf2dd8b000ac195dd9ebba4fb344dc5baa (patch) | |
| tree | 18072db815a8f417c4d63a00ae95286642f0dff3 /scripts/cleanEnvironmentHelper.ps1 | |
| parent | 34257a50ceda0bfa05e59b532f71223b70f39d52 (diff) | |
| parent | 842252373992a9c7b74f041607b47754d61cc0c8 (diff) | |
| download | vcpkg-99eb78cf2dd8b000ac195dd9ebba4fb344dc5baa.tar.gz vcpkg-99eb78cf2dd8b000ac195dd9ebba4fb344dc5baa.zip | |
Merge https://github.com/Microsoft/vcpkg
Diffstat (limited to 'scripts/cleanEnvironmentHelper.ps1')
| -rw-r--r-- | scripts/cleanEnvironmentHelper.ps1 | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/scripts/cleanEnvironmentHelper.ps1 b/scripts/cleanEnvironmentHelper.ps1 new file mode 100644 index 000000000..0a133f5f8 --- /dev/null +++ b/scripts/cleanEnvironmentHelper.ps1 @@ -0,0 +1,52 @@ +# Capture environment variables for the System and User. Also add some special/built-in variables. +# These will be used to synthesize a clean environment +$specialEnvironmentMap = @{ "SystemDrive"=$env:SystemDrive; "SystemRoot"=$env:SystemRoot; "UserProfile"=$env:UserProfile } # These are built-in and not set in the registry +$machineEnvironmentMap = [Environment]::GetEnvironmentVariables('Machine') # HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment +$userEnvironmentMap = [Environment]::GetEnvironmentVariables('User') # HKEY_CURRENT_USER\Environment + +# Identify the keySet of environment variable names +$nameSet = ($specialEnvironmentMap.Keys + $machineEnvironmentMap.Keys + $userEnvironmentMap.Keys) | Sort-Object | Select-Object -Unique + +# Any environment variable in the $nameSet should be restored to its original value +foreach ($name in $nameSet) +{ + if ($specialEnvironmentMap.ContainsKey($name)) + { + [Environment]::SetEnvironmentVariable($name, $specialEnvironmentMap[$name], 'Process') + continue; + } + + # PATH needs to be concatenated as it has values in both machine and user environment. Any other values should be set. + if ($name -match 'path') + { + $pathValuePartial = @() + # Machine values before user values + $pathValuePartial += $machineEnvironmentMap[$name] -split ';' + $pathValuePartial += $userEnvironmentMap[$name] -split ';' + $pathValue = $pathValuePartial -join ';' + [Environment]::SetEnvironmentVariable($name, $pathValue, 'Process') + continue; + } + + if ($userEnvironmentMap.ContainsKey($name)) + { + [Environment]::SetEnvironmentVariable($name, $userEnvironmentMap[$name], 'Process') + continue; + } + + if ($machineEnvironmentMap.ContainsKey($name)) + { + [Environment]::SetEnvironmentVariable($name, $machineEnvironmentMap[$name], 'Process') + continue; + } + + throw "Unreachable: Unknown variable $name" +} + +# Any environment variable NOT in the $nameSet should be removed +$processEnvironmentMap = [Environment]::GetEnvironmentVariables('Process') +$variablesForRemoval = $processEnvironmentMap.Keys | Where-Object {$nameSet -notcontains $_} +foreach ($name in $variablesForRemoval) +{ + [Environment]::SetEnvironmentVariable($name, $null, 'Process') +} |
