From a12eff96c05956a442fab8075dea7c2461bb142d Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Thu, 28 Jul 2016 18:18:18 -0700 Subject: [PATCH] Change formatter for ErrorRecord Fix #1465 On Linux, it's common to use error stream (2) as an information stream. There is no errors in this case, it's just a way to report info. When PowerShell redirects error stream, it wraps all output from stream 2 in NativeCommandError ErrorRecord. That doesn't happen for interactive experience (when there is no redirection), but it shows up in logs, scripting and non-console powershell hosts (i.e. PowerShell ISE). Here we change the formatter, so we are keeping the ErrorRecords objects, but we are changing their default representation. If it's a NativeCommandError, we don't use the regular ErrorRecord formatter. It allows us keep ErrorRecord object, but in redirection case it will look exectly like on the screen. Also, we are change NativeCommandErrorMessage to use one line per ErrorRecord to enable this scenario. --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 4 ++-- .../engine/NativeCommandProcessor.cs | 10 ++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 84c2d371a9..47dd76fabd 100644 --- a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -720,7 +720,7 @@ namespace System.Management.Automation.Runspaces CustomControl.Create(outOfBand: true) .StartEntry() .AddScriptBlockExpressionBinding(@" - if ($_.FullyQualifiedErrorId -ne ""NativeCommandErrorMessage"" -and $ErrorView -ne ""CategoryView"") + if (($_.FullyQualifiedErrorId -ne ""NativeCommandErrorMessage"" -and $_.FullyQualifiedErrorId -ne ""NativeCommandError"") -and $ErrorView -ne ""CategoryView"") { $myinv = $_.InvocationInfo if ($myinv -and $myinv.MyCommand) @@ -767,7 +767,7 @@ namespace System.Management.Automation.Runspaces } ") .AddScriptBlockExpressionBinding(@" - if ($_.FullyQualifiedErrorId -eq ""NativeCommandErrorMessage"") { + if ($_.FullyQualifiedErrorId -eq ""NativeCommandErrorMessage"" -or $_.FullyQualifiedErrorId -eq ""NativeCommandError"") { $_.Exception.Message } else diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 507a5bb7ba..8971dd6446 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1880,17 +1880,11 @@ namespace System.Management.Automation // // Wrap the rest of the output in ErrorRecords with the "NativeCommandErrorMessage" error ID // - char[] buffer = new char[4096]; - - int read = 0; - - while ((read = _streamReader.Read(buffer, 0, buffer.Length)) != 0) + while ((line = _streamReader.ReadLine()) != null) { - StringBuilder errorMessage = new StringBuilder().Append(buffer, 0, read); - AddObjectToWriter( new ErrorRecord( - new RemoteException(errorMessage.ToString()), + new RemoteException(line), "NativeCommandErrorMessage", ErrorCategory.NotSpecified, null),