From 0c91e15c33be72b42fd0bd4e086fd41abda50184 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Thu, 22 Jun 2023 12:01:30 -0400 Subject: [PATCH 1/5] Fix native executables not redirecting to file --- .../engine/NativeCommandProcessor.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 10c4dda1e5c..917ba20881e 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1594,8 +1594,12 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect // $powershell.AddScript('ipconfig.exe') // $powershell.AddCommand('Out-Default') // $powershell.Invoke()) - // we should not count it as a redirection. - if (IsDownstreamOutDefault(this.commandRuntime.OutputPipe)) + // we should not count it as a redirection. Unless the native command has it's stdout redirected + // for example: + // cmd.exe /c "echo test" > somefile.log + // in that case we want to keep output redirection even though Out-Default is the only + // downstream command. + if (IsDownstreamOutDefault(this.commandRuntime.OutputPipe) && StdOutDestination is null) { redirectOutput = false; } @@ -1612,7 +1616,9 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect // $powershell.AddScript('ipconfig.exe') // $powershell.AddCommand('Out-Default') // $powershell.Invoke()) - // we should not count that as a redirection. + // we should not count that as a redirection. We do not need to worry + // about StdOutDestination here as if error is redirected then it's assumed + // to be text based and Out-File will be added to the pipeline instead. if (IsDownstreamOutDefault(this.commandRuntime.ErrorOutputPipe)) { redirectError = false; From 3714e9735f58dbfbca69bbfb4a05b7a09c860bcd Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 22 Jun 2023 10:08:12 -0700 Subject: [PATCH 2/5] Update NativeCommandProcessor.cs --- .../engine/NativeCommandProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 917ba20881e..d17688100b3 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1606,7 +1606,7 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect } // See if the error output stream has been redirected, either through an explicit 2> foo.txt or - // my merging error into output through 2>&1. + // by merging error into output through 2>&1. if (CommandRuntime.ErrorMergeTo != MshCommandRuntime.MergeDataStream.Output) { // If the error output pipe is the default outputter, for example, calling the native command from command-line host, From c6a863e7f6df527aa2555b99f999b6cd3e0db5d9 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Thu, 22 Jun 2023 14:00:53 -0400 Subject: [PATCH 3/5] Update src/System.Management.Automation/engine/NativeCommandProcessor.cs Co-authored-by: Dongbo Wang --- .../engine/NativeCommandProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index d17688100b3..b511054a051 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1594,7 +1594,7 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect // $powershell.AddScript('ipconfig.exe') // $powershell.AddCommand('Out-Default') // $powershell.Invoke()) - // we should not count it as a redirection. Unless the native command has it's stdout redirected + // we should not count it as a redirection. Unless the native command has its stdout redirected // for example: // cmd.exe /c "echo test" > somefile.log // in that case we want to keep output redirection even though Out-Default is the only From 3ed14756244831704cad1c2262b00f2beba077b5 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Thu, 22 Jun 2023 15:39:54 -0400 Subject: [PATCH 4/5] Add tests for redirection --- .../engine/Basic/NativeCommandBytePiping.Tests.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 b/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 index 8126c08473f..fb2aebef5c3 100644 --- a/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 +++ b/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 @@ -63,4 +63,13 @@ Describe 'Native command byte piping tests' -Tags 'CI' { ($pipe)?.Dispose() } } + + It 'Bytes are retained when redirecting to a file' { + testexe -writebytes FF > $TestDrive/content.bin | Should -BeNullOrEmpty + Get-Content -LiteralPath $TestDrive/content.bin -AsByteStream | Should -Be 0xFFuy + } + + It 'Redirecting to $null should emit no output' { + testexe -writebytes FF > $null | Should -BeNullOrEmpty + } } From 8faa66735f8a165a2ff519c0a0437e03003b2ff1 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Fri, 23 Jun 2023 11:43:41 -0400 Subject: [PATCH 5/5] Added test with `Out-Default` --- .../engine/Basic/NativeCommandBytePiping.Tests.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 b/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 index fb2aebef5c3..1e645df9030 100644 --- a/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 +++ b/test/powershell/engine/Basic/NativeCommandBytePiping.Tests.ps1 @@ -69,6 +69,11 @@ Describe 'Native command byte piping tests' -Tags 'CI' { Get-Content -LiteralPath $TestDrive/content.bin -AsByteStream | Should -Be 0xFFuy } + It 'Bytes are retained when redirecting to a file and Out-Default is downstream' { + testexe -writebytes FF > $TestDrive/content2.bin | Out-Default + Get-Content -LiteralPath $TestDrive/content2.bin -AsByteStream | Should -Be 0xFFuy + } + It 'Redirecting to $null should emit no output' { testexe -writebytes FF > $null | Should -BeNullOrEmpty }