diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 756e294b226..87a65dfdcbd 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -1215,7 +1215,7 @@ public override void WriteDebugLine(string message) { if (SupportsVirtualTerminal) { - WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Debug) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset); + WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Debug, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.DebugFormatString, message) + PSStyle.Instance.Reset); } else { @@ -1276,7 +1276,7 @@ public override void WriteVerboseLine(string message) { if (SupportsVirtualTerminal) { - WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Verbose) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset); + WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Verbose, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.VerboseFormatString, message) + PSStyle.Instance.Reset); } else { @@ -1320,7 +1320,7 @@ public override void WriteWarningLine(string message) { if (SupportsVirtualTerminal) { - WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Warning) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset); + WriteLine(Utils.GetFormatStyleString(Utils.FormatStyle.Warning, Console.IsOutputRedirected) + StringUtil.Format(ConsoleHostUserInterfaceStrings.WarningFormatString, message) + PSStyle.Instance.Reset); } else { diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index eb2470e6d17..91f6083596d 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1617,13 +1617,12 @@ internal enum FormatStyle Debug, } - internal static string GetFormatStyleString(FormatStyle formatStyle) + internal static string GetFormatStyleString(FormatStyle formatStyle, bool isOutputRedirected) { // redirected console gets plaintext output to preserve existing behavior if (!InternalTestHooks.BypassOutputRedirectionCheck && ((PSStyle.Instance.OutputRendering == OutputRendering.PlainText) || - (formatStyle == FormatStyle.Error && Console.IsErrorRedirected) || - (formatStyle != FormatStyle.Error && Console.IsOutputRedirected))) + isOutputRedirected)) { return string.Empty; } diff --git a/test/powershell/engine/Formatting/OutputRendering.Tests.ps1 b/test/powershell/engine/Formatting/OutputRendering.Tests.ps1 index 106197b10f5..14085cc23ba 100644 --- a/test/powershell/engine/Formatting/OutputRendering.Tests.ps1 +++ b/test/powershell/engine/Formatting/OutputRendering.Tests.ps1 @@ -2,14 +2,6 @@ # Licensed under the MIT License. Describe 'OutputRendering tests' { - BeforeAll { - $th = New-TestHost - $rs = [runspacefactory]::Createrunspace($th) - $rs.open() - $ps = [powershell]::Create() - $ps.Runspace = $rs - } - BeforeEach { if ($null -ne $PSStyle) { $oldOutputRendering = $PSStyle.OutputRendering @@ -20,8 +12,6 @@ Describe 'OutputRendering tests' { if ($null -ne $PSStyle) { $PSStyle.OutputRendering = $oldOutputRendering } - - $ps.Commands.Clear() } It 'OutputRendering works for "" to the host' -TestCases @(