Skip to content

pwsh ignores -OutputFormat text when writing to a redirected stderr stream #5912

Closed

Description

@brantb

Steps to reproduce

$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
$p.StandardError.ReadToEnd()

Expected behavior

The error is written to stderr as text:

boom
At line:1 char:1
+ throw 'boom'
+ ~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (boom:String) [], RuntimeException
+ FullyQualifiedErrorId : boom

Changing $p.StartInfo.RedirectStandardError to $false (the default) will result in the expected output.

Actual behavior

The error record is encoded as CLIXML before being written to stderr:

#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">boom_x000D__x000A_</S><S S="Error">At line:1 char:1_x000D__x000A_</S><S S="Error">+ throw 'boom'_x000D__x000A_</S><S S="Error">+ ~~~~~~~~~~~~_x000D__x000A_</S><S S="Error">+ CategoryInfo          : OperationStopped: (boom:String) [], RuntimeException_x000D__x000A_</S><S S="Error">+ FullyQualifiedErrorId : boom_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>

This problem doesn't seem to apply to stdout. Setting $p.StartInfo.RedirectStandardOutput = $true in a similar fashion works as expected.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0
PSEdition                      Core
GitCommitId                    v6.0.0
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Discussion
    on Jan 17, 2018
  2. bugoutfr commented on May 14, 2018

    @bugoutfr

    Hi,
    I have the same problem here. The parent Powershell script is launched by Gitlab Runner which doesn't output to the standard output. With this configuration I end up with a messy mixed XMLCLI and normal text output instead of only the requested Text output.

    Sample command line :

    pwsh.exe -NoLogo -NonInteractive -NoProfile -OutputFormat Text -Command { [...] }

    Regards.

  3. MartinSGill commented on Oct 11, 2018

    @MartinSGill

    Any progress on this? It's really annoying in our build logs. I'd love to try and fix this myself.. but I've no idea where to look :(.

  4. MartinSGill commented on Oct 23, 2018

    @MartinSGill

    Seems to have happened before and seems related to -EncodedCommand

    hashicorp/packer#4322

  5. SteveL-MSFT commented on Oct 24, 2018

    @SteveL-MSFT
    Member

    Looking in the code it appears this is by-design, but I don't know why this is the case. Jason Shirk (@lzybkr) do you recall or know why we force XML in this case?

  6. lzybkr commented on Oct 24, 2018

    @lzybkr
    Contributor

    I don't recall any history (the code comes from V1 I believe, before my time).

    I'm guessing that code ignored the case where OutputFormat was specified explicitly. It makes sense to implicitly change to XML if OutputFormat is not specified - with the assumption being that you're piping PowerShell to PowerShell.

  7. SteveL-MSFT commented on Oct 24, 2018

    @SteveL-MSFT
    Member

    Ok, that makes sense. So I can just modify it so that if OutputFormat was specified explicitly, we should honor that.

  8. TishSerg commented on Apr 12, 2022

    @TishSerg

    How about this fix for PowerShell 5.1.* branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-DiscussionResolution-Fixed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions