From 13f8ba284fc8bbbdf981f01dc87758878fe7c0d6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 18 Oct 2019 22:09:44 -0700 Subject: [PATCH] Set the output encoding to [Console]::OutputEncoding for native commands (#10824) --- .../engine/NativeCommandProcessor.cs | 2 ++ .../NativeExecution/NativeCommandProcessor.Tests.ps1 | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 8883cb8c2d..7a7ab4a3b8 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1101,11 +1101,13 @@ namespace System.Management.Automation if (redirectOutput) { startInfo.RedirectStandardOutput = true; + startInfo.StandardOutputEncoding = Console.OutputEncoding; } if (redirectError) { startInfo.RedirectStandardError = true; + startInfo.StandardErrorEncoding = Console.OutputEncoding; } } else diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index b9a3f7f3bd..dbc0a07469 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -128,6 +128,18 @@ Describe "Native Command Processor" -tags "Feature" { $ps.Dispose() } } + + It "OutputEncoding should be used" -Skip:(!$IsWindows -or !(Get-Command sfc.exe)) { + + $originalOutputEncoding = [Console]::OutputEncoding + try { + [Console]::OutputEncoding = [System.Text.Encoding]::Unicode + sfc | Out-String | Should -Not -Match "`0" + } + finally { + [Console]::OutputEncoding = $originalOutputEncoding + } + } } Describe "Open a text file with NativeCommandProcessor" -tags @("Feature", "RequireAdminOnWindows") {