Skip to content

Commit 7a43e6f

Browse files
committed
(GH-188) Filter current environment variables
When Protected Event Logging and PowerShell Script Block logging are enabled together the SystemRoot environment variable is a requirement. If it is removed as part of the environment purge it causes the PowerShell process to crash, therefore breaking the pipe between Ruby and the remote PowerShell session. This commit fixes the issue by creating a filtered list of environment variables which does not include the SystemRoot environment variable. This list can then be purged safely.
1 parent 4ce17ae commit 7a43e6f

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/templates/init.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ $global:ourFunctions = @'
3535
function Reset-ProcessEnvironmentVariables {
3636
param($CachedEnvironmentVariables)
3737
38+
# When Protected Event Logging and PowerShell Script Block logging are enabled together
39+
# the SystemRoot environment variable is a requirement. If it is removed as part of this purge
40+
# it causes the PowerShell process to crash, therefore breaking the pipe between Ruby and the
41+
# remote PowerShell session.
42+
# The least descructive way to avoid this is to filter out SystemRoot when pulling our current list
43+
# of environment variables. Then we can continue safely with the removal.
44+
$CurrentEnvironmentVariables = Get-ChildItem -Path Env:\* |
45+
Where-Object {$_.Name -ne "SystemRoot"}
46+
3847
# Delete existing environment variables
39-
Remove-Item -Path Env:\* -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Recurse
48+
Remove-Item -Path $CurrentEnvironmentVariables -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Recurse
4049
4150
# Re-add the cached environment variables
4251
$CachedEnvironmentVariables |

0 commit comments

Comments
 (0)