Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1908,9 +1908,13 @@ protected override void BeginProcessing()

if (_UseNewEnvironment)
{
startInfo.EnvironmentVariables.Clear();
LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine));
LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User));
// Use environment variables from saved at startup dictionary
var envVariables = startInfo.EnvironmentVariables;
envVariables.Clear();
foreach (DictionaryEntry entry in System.Management.Automation.Runspaces.EarlyStartup.InitialEnvironmentVariables)
{
envVariables.Add((string)entry.Key, (string)entry.Value);
}
}

startInfo.WindowStyle = _windowstyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public sealed class UnmanagedPSEntry
/// </param>
public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc)
{
// Save environment variables before we change them.
EarlyStartup.SaveEnvironmentVariables();

// Warm up some components concurrently on background threads.
EarlyStartup.Init();

Expand Down
12 changes: 12 additions & 0 deletions src/System.Management.Automation/engine/InitialSessionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace System.Management.Automation.Runspaces
{
internal class EarlyStartup
{
private static IDictionary _environmentVariables;

internal static void Init()
{
// Code added here should:
Expand Down Expand Up @@ -65,6 +67,16 @@ internal static void Init()
LanguagePrimitives.GetEnumerator(null);
});
}

internal static IDictionary InitialEnvironmentVariables
{
get => _environmentVariables;
}

internal static void SaveEnvironmentVariables()
{
_environmentVariables = Environment.GetEnvironmentVariables();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ Describe "Start-Process" -Tag "Feature","RequireAdminOnWindows" {
}
}

Describe "Start-Process" -Tags "CI" {

It "UseNewEnvironment parameter should restore environment variables for child process" {
$TestVariableName = "TestVariableName"
[Environment]::SetEnvironmentVariable($TestVariableName, 1)

if ($IsWindows) {
Start-Process -Wait -ArgumentList '/c','set' cmd.exe -RedirectStandardOutput "$TESTDRIVE/outputBefore"
Start-Process -Wait -ArgumentList '/c','set' cmd.exe -UseNewEnvironment -RedirectStandardOutput "$TESTDRIVE/outputAfter"
} else {
Start-Process -Wait -ArgumentList '--norc','-c','printenv' bash -RedirectStandardOutput "$TESTDRIVE/outputBefore"
Start-Process -Wait -ArgumentList '--norc','-c','printenv' bash -UseNewEnvironment -RedirectStandardOutput "$TESTDRIVE/outputAfter"
}

$before = Get-Content -Raw -Path "$TESTDRIVE/outputBefore"
$after = Get-Content -Raw -Path "$TESTDRIVE/outputAfter"

$before | Should -Not -BeExactly $after
$before | Should -Match $TestVariableName
}
}

Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWindows" {

BeforeEach {
Expand Down