Skip to content
Merged
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 @@ -349,6 +349,16 @@ internal Serialization.DataFormat OutputFormat
}
}

internal bool OutputFormatSpecified
{
get
{
Dbg.Assert(_dirty, "Parse has not been called yet");

return _outputFormatSpecified;
}
}

internal Serialization.DataFormat InputFormat
{
get
Expand Down Expand Up @@ -475,7 +485,7 @@ private static string GetConfigurationNameFromGroupPolicy()
// Current user policy takes precedence.
var consoleSessionSetting = Utils.GetPolicySetting<ConsoleSessionConfiguration>(Utils.CurrentUserThenSystemWideConfig);

return (consoleSessionSetting?.EnableConsoleSessionConfiguration == true && !string.IsNullOrEmpty(consoleSessionSetting?.ConsoleSessionConfigurationName)) ?
return (consoleSessionSetting?.EnableConsoleSessionConfiguration == true && !string.IsNullOrEmpty(consoleSessionSetting?.ConsoleSessionConfigurationName)) ?
consoleSessionSetting.ConsoleSessionConfigurationName : string.Empty;
}

Expand All @@ -493,7 +503,7 @@ private static void EarlyParseHelper(string[] args)
Dbg.Assert(args != null, "Argument 'args' to EarlyParseHelper should never be null");
return;
}

bool noexitSeen = false;
for (int i = 0; i < args.Length; ++i)
{
Expand Down Expand Up @@ -587,7 +597,7 @@ private static bool MatchSwitch(string switchKey, string match, string smallestU
Dbg.Assert(smallestUnambiguousMatch.Trim().ToLowerInvariant() == smallestUnambiguousMatch, "match should be normalized to lowercase w/ no outside whitespace");
Dbg.Assert(match.Contains(smallestUnambiguousMatch), "sUM should be a substring of match");

return (match.Trim().ToLowerInvariant().IndexOf(switchKey, StringComparison.Ordinal) == 0 &&
return (match.Trim().ToLowerInvariant().IndexOf(switchKey, StringComparison.Ordinal) == 0 &&
switchKey.Length >= smallestUnambiguousMatch.Length);
}

Expand Down Expand Up @@ -849,6 +859,7 @@ private void ParseHelper(string[] args)
else if (MatchSwitch(switchKey, "outputformat", "o") || MatchSwitch(switchKey, "of", "o"))
{
ParseFormat(args, ref i, ref _outFormat, CommandLineParameterParserStrings.MissingOutputFormatParameter);
_outputFormatSpecified = true;
}
else if (MatchSwitch(switchKey, "inputformat", "in") || MatchSwitch(switchKey, "if", "if"))
{
Expand Down Expand Up @@ -1347,6 +1358,7 @@ private bool CollectArgs(string[] args, ref int i)
private uint _exitCode = ConsoleHost.ExitCodeSuccess;
private bool _dirty;
private Serialization.DataFormat _outFormat = Serialization.DataFormat.Text;
private bool _outputFormatSpecified = false;
private Serialization.DataFormat _inFormat = Serialization.DataFormat.Text;
private Collection<CommandParameter> _collectedArgs = new Collection<CommandParameter>();
private string _file;
Expand Down
8 changes: 5 additions & 3 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ internal RunspaceRef RunspaceRef
}

internal WrappedSerializer.DataFormat OutputFormat { get; private set; }
internal bool OutputFormatSpecified { get; private set; }

internal WrappedSerializer.DataFormat InputFormat { get; private set; }

Expand All @@ -1211,9 +1212,9 @@ internal WrappedDeserializer.DataFormat ErrorFormat
{
WrappedDeserializer.DataFormat format = OutputFormat;

//If this shell is invoked in minishell interop mode and error is redirected,
//always write data in error stream in xml format.
if (IsInteractive == false && Console.IsErrorRedirected && _wasInitialCommandEncoded)
// If this shell is invoked in non-interactive, error is redirected, and OutputFormat was not
// specified write data in error stream in xml format assuming PowerShell->PowerShell usage.
if (!OutputFormatSpecified && IsInteractive == false && Console.IsErrorRedirected && _wasInitialCommandEncoded)
{
format = Serialization.DataFormat.XML;
}
Expand Down Expand Up @@ -1323,6 +1324,7 @@ private uint Run(CommandLineParameterParser cpp, bool isPrestartWarned)
}

OutputFormat = cpp.OutputFormat;
OutputFormatSpecified = cpp.OutputFormatSpecified;
InputFormat = cpp.InputFormat;
_wasInitialCommandEncoded = cpp.WasInitialCommandEncoded;

Expand Down
13 changes: 13 additions & 0 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
# Join (multiple lines) and remove whitespace (we don't care about spacing) to verify we converted to string (by generating a table)
-join (& $powershell -noprofile -outputFormat text { [PSCustomObject]@{X=10;Y=20} }) -replace "\s","" | Should -Be "XY--1020"
}

It "errors are in text if error is redirected, encoded command, non-interactive, and outputformat specified" {
$p = [Diagnostics.Process]::new()
$p.StartInfo.FileName = "pwsh"
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("throw 'boom'"))
$p.StartInfo.Arguments = "-EncodedCommand $encoded -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -OutputFormat text"
$p.StartInfo.UseShellExecute = $false
$p.StartInfo.RedirectStandardError = $true
$p.Start() | Out-Null
$out = $p.StandardError.ReadToEnd()
$out | Should -Not -BeNullOrEmpty
$out.Split([Environment]::NewLine)[0] | Should -BeExactly "boom"
}
}

Context "Redirected standard output" {
Expand Down