Skip to content
Merged
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 @@ -1823,6 +1823,26 @@ public SwitchParameter UseNewEnvironment

private SwitchParameter _UseNewEnvironment;

/// <summary>
/// Gets or sets the environment variables for the process.
/// </summary>
[Parameter]
public Hashtable Environment
{
get
{
return _environment;
}

set
{
_environment = value;
_isDefaultSetParameterSpecified = true;
}
}

private Hashtable _environment;

#endregion

#region overrides
Expand Down Expand Up @@ -1929,8 +1949,13 @@ protected override void BeginProcessing()
if (_UseNewEnvironment)
{
startInfo.EnvironmentVariables.Clear();
LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine));
LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User));
LoadEnvironmentVariable(startInfo, System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine));
LoadEnvironmentVariable(startInfo, System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User));
}

if (_environment != null)
{
LoadEnvironmentVariable(startInfo, _environment);
}

startInfo.WindowStyle = _windowstyle;
Expand Down Expand Up @@ -2176,13 +2201,20 @@ private static void LoadEnvironmentVariable(ProcessStartInfo startinfo, IDiction
processEnvironment.Remove(entry.Key.ToString());
}

if (entry.Key.ToString().Equals("PATH"))
if (entry.Value != null)
{
processEnvironment.Add(entry.Key.ToString(), Environment.GetEnvironmentVariable(entry.Key.ToString(), EnvironmentVariableTarget.Machine) + ";" + Environment.GetEnvironmentVariable(entry.Key.ToString(), EnvironmentVariableTarget.User));
}
else
{
processEnvironment.Add(entry.Key.ToString(), entry.Value.ToString());
if (entry.Key.ToString().Equals("PATH"))
{
#if UNIX
processEnvironment.Add(entry.Key.ToString(), entry.Value.ToString());
#else
processEnvironment.Add(entry.Key.ToString(), entry.Value.ToString() + Path.PathSeparator + System.Environment.GetEnvironmentVariable(entry.Key.ToString(), EnvironmentVariableTarget.Machine) + Path.PathSeparator + System.Environment.GetEnvironmentVariable(entry.Key.ToString(), EnvironmentVariableTarget.User));
#endif
}
else
{
processEnvironment.Add(entry.Key.ToString(), entry.Value.ToString());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWi
}
}

Describe "Start-Process" -Tags "Feature" {
Describe "Environment Tests" -Tags "Feature" {

It "UseNewEnvironment parameter should reset environment variables for child process" {

Expand All @@ -206,4 +206,33 @@ Describe "Start-Process" -Tags "Feature" {
$env:TestEnvVariable = $null
}
}

It '-Environment adds or replaces environment variables to child process' {
$outputfile = Join-Path -Path $TestDrive -ChildPath output.txt
Start-Process pwsh -ArgumentList '-NoProfile','-Nologo','-OutputFormat xml','-Command get-childitem env:' -Wait -Environment @{ a = 1; B = 'hello'; TERM = 'dumb'; PATH = 'mine' } -RedirectStandardOutput $outputfile
$out = Import-Clixml $outputfile
($out | Where-Object { $_.Name -eq 'a' }).Value | Should -Be 1
($out | Where-Object { $_.Name -eq 'B' }).Value | Should -BeExactly 'hello'
($out | Where-Object { $_.Name -eq 'TERM' }).Value | Should -BeExactly 'dumb'
$pathSeparator = [System.IO.Path]::PathSeparator
if ($IsWindows) {
($out | Where-Object { $_.Name -eq 'PATH' }).Value | Should -BeLike "*${pathSeparator}mine${pathSeparator}*"
} else {
($out | Where-Object { $_.Name -eq 'PATH' }).Value | Should -BeLike "*${pathSeparator}mine"
}
}

It '-Environment can remove an environment variable from child process' {
try {
$env:existing = 1 # set a variable that we will remove
$env:nonexisting = $null # validate that removing a non-existing variable is a no-op
$outputfile = Join-Path -Path $TestDrive -ChildPath output.txt
Start-Process pwsh -ArgumentList '-NoProfile','-Nologo','-OutputFormat xml','-Command get-childitem env:' -Wait -Environment @{ existing = $null; nonexisting = $null } -RedirectStandardOutput $outputfile
$out = Import-Clixml $outputfile
$out | Where-Object { $_.Name -eq 'existing' } | Should -BeNullOrEmpty
$out | Where-Object { $_.Name -eq 'nonexisting' } | Should -BeNullOrEmpty
} finally {
$env:existing = $null
}
}
}