Skip to content
Open
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 @@ -2034,13 +2034,6 @@ protected override void BeginProcessing()
{
_redirectstandarderror = ResolveFilePath(_redirectstandarderror);
_redirectstandardoutput = ResolveFilePath(_redirectstandardoutput);
if (_redirectstandardoutput.Equals(_redirectstandarderror, StringComparison.OrdinalIgnoreCase))
{
message = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardOutput", "RedirectStandardError");
ErrorRecord er = new(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null);
WriteError(er);
return;
}
}
}
else if (ParameterSetName.Equals("UseShellExecute"))
Expand Down Expand Up @@ -2309,8 +2302,14 @@ private void SetupInputOutputRedirection(Process p)
_redirectstandarderror = ResolveFilePath(_redirectstandarderror);
p.ErrorDataReceived += new DataReceivedEventHandler(StdErrorHandler);

// Can't do StreamWriter(string) in coreCLR
_errorWriter = new StreamWriter(new FileStream(_redirectstandarderror, FileMode.Create));
if (_redirectstandarderror.Equals(_redirectstandardoutput, StringComparison.OrdinalIgnoreCase) && _outputWriter != null) {
_errorWriter = _outputWriter;
}
else
{
// Can't do StreamWriter(string) in coreCLR
_errorWriter = new StreamWriter(new FileStream(_redirectstandarderror, FileMode.Create));
}
}
else
{
Expand Down Expand Up @@ -2430,7 +2429,14 @@ private void SetStartupInfo(ProcessStartInfo startinfo, ref ProcessNativeMethods
hasRedirection = true;
startinfo.RedirectStandardError = true;
_redirectstandarderror = ResolveFilePath(_redirectstandarderror);
lpStartupInfo.hStdError = GetSafeFileHandleForRedirection(_redirectstandarderror, FileMode.Create);
if (_redirectstandarderror.Equals(_redirectstandardoutput, StringComparison.OrdinalIgnoreCase) && lpStartupInfo.hStdOutput != null)
{
lpStartupInfo.hStdError = lpStartupInfo.hStdOutput;
}
else
{
lpStartupInfo.hStdError = GetSafeFileHandleForRedirection(_redirectstandarderror, FileMode.Create);
}
}

if (hasRedirection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ Describe "Start-Process" -Tag "Feature","RequireAdminOnWindows" {
$tempDirectory = Join-Path -Path $TestDrive -ChildPath 'PSPath[]'
New-Item $tempDirectory -ItemType Directory -Force
$assetsFile = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath assets) -ChildPath SortTest.txt
$PWSH = (Get-Process -Id $PID).MainModule.FileName
$pwshParam = "-NoProfile -Command &{
[Console]::Out.WriteLine('stdout1');
[Console]::Error.WriteLine('stderr1');
[Console]::Out.WriteLine('stdout2');
[Console]::Error.WriteLine('stderr2')
}"
$nl = [System.Environment]::NewLine
if ($IsWindows) {
$pingParam = "-n 2 localhost"
}
Expand Down Expand Up @@ -82,17 +90,26 @@ Describe "Start-Process" -Tag "Feature","RequireAdminOnWindows" {
}

It "Should handle stderr redirection without error" {
$process = Start-Process ping -ArgumentList $pingParam -PassThru -RedirectStandardError $tempFile -RedirectStandardOutput "$TESTDRIVE/output" @extraArgs

$process.Length | Should -Be 1
$process.Id | Should -BeGreaterThan 1
# $process.ProcessName | Should -Be "ping"
$output = & $PWSH -NoProfile -Command "&{ Start-Process $PWSH -ArgumentList `"$pwshParam`" -Wait -NoNewWindow -RedirectStandardError $tempFile }"
$output | Should -BeExactly @("stdout1", "stdout2")
Get-Content -Raw -Path $tempFile | Should -BeExactly "stderr1${nl}stderr2${nl}"
}

It "Should handle stdout redirection without error" {
$process = Start-Process ping -ArgumentList $pingParam -Wait -RedirectStandardOutput $tempFile @extraArgs
$dirEntry = Get-ChildItem $tempFile
$dirEntry.Length | Should -BeGreaterThan 0
$output = & $PWSH -NoProfile -Command "&{ Start-Process $PWSH -ArgumentList `"$pwshParam`" -Wait -NoNewWindow -RedirectStandardOutput $tempFile }" 2>&1
$output | Should -BeExactly @("stderr1", "stderr2")
Get-Content -Raw -Path $tempFile | Should -BeExactly "stdout1${nl}stdout2${nl}"
}

It "Should handle stdout,stderr redirections without error" {
Start-Process $PWSH -ArgumentList $pwshParam -Wait -RedirectStandardError $tempFile -RedirectStandardOutput "$TESTDRIVE/output"
Get-Content -Raw -Path "$TESTDRIVE/output" | Should -BeExactly "stdout1${nl}stdout2${nl}"
Get-Content -Raw -Path $tempFile | Should -BeExactly "stderr1${nl}stderr2${nl}"
}

It "Should handle stdout,stderr redirections to the same file without error" {
Start-Process $PWSH -ArgumentList $pwshParam -Wait -RedirectStandardError $tempFile -RedirectStandardOutput $tempFile
Get-Content -Raw -Path $tempFile | Should -BeExactly "stdout1${nl}stderr1${nl}stdout2${nl}stderr2${nl}"
}

It "Should handle stdin redirection without error" {
Expand Down