Skip to content

CLI: -OutputFormat XML situationally produces broken XML (CLIXML) #12489

Closed

Description

The CLI's -OutputFormat Xml (-o Xml / -of Xml) parameter produces CLIXML output that can later be read with Import-CliXml, for instance.

If a -Command / -c command string / a *.ps1 script file passed to -File writes to multiple streams, the output XML text is broken in two (possibly related) ways:

  • The single magic #< CLIXML comment that is usually on the first line only is doubled (that is, the 2nd line contains the same comment, with only the 3rd line containing the XML).

  • A 2nd top-level <Objs> root element appears, seemingly covering the non-success streams separately, which results in broken XML as well as a loss of the ordering of output objects.

As an aside: it seems that host output is captured as success-stream output (S="Output") rather than information-stream output (S="information").
Is this by (historical) design, retained for backward compatibility?

Steps to reproduce

{ 
 $ErrorActionPreference = 'Stop'
 pwsh -o xml -noprofile -command 'Write-Output output!; Write-Verbose -vb verbose!' | 
   Write-Output 
} | Should -not -Throw

Expected behavior

The test should succeed - no errors should occur.

Note that the test relies on the fact that PowerShell automatically deserializes CLIXML text when you send it through the pipeline, so that Write-Output outputs the deserialized ("rehydrated") output objects.

Actual behavior

The following error occurs:

Expected no exception to be thrown, but an exception 
"Cannot process the XML from the 'Output' stream of '/.../pwsh': 
Data at the root level is invalid. Line 1, position 1." was
thrown from line:1 char:36 ...

If you want to examine the actual XML, run the following on Unix:

sh -c 'pwsh -o xml -noprofile -command ''Write-Output output!; Write-Verbose -vb verbose!'' > out.xml'

You'll see the following content in out.xml - note the doubled comment line and the presence of two root elements (broken XML):

#< CLIXML
#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
	<S S="Output">
		output!
	</S>
</Objs>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
	<S S="verbose">
		verbose!
	</S>
</Objs>

Environment data

PowerShell Core 7.1.0-preview.2

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Apr 25, 2020
  2. changed the title [-]CLI: -OutputFormat XML situationally produces broken XML[/-] [+]CLI: -OutputFormat XML situationally produces broken XML (CLIXML)[/+] on Apr 25, 2020
  3. vexx32 commented on Apr 25, 2020

    @vexx32
    Collaborator

    I've seen this turn up in a few places, but wasn't sure what exactly was causing it. Thanks for the detailed investigation as always! 😊 💖

  4. added
    Issue-BugIssue has been identified as a bug in the product
    WG-Enginecore PowerShell engine, interpreter, and runtime
    and removed
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Jan 16, 2021
  5. Jaykul commented on Mar 31, 2022

    @Jaykul
    Contributor

    Another very baffling side-effect of this, is when you get only error output. So for instance:

    $Result = pwsh -OutputFormat Xml -Command Get-Typo | Where Text -eq "X"

    You expect a #< CLIXML header to mean the output gets converted to objects and you'll capture it in $Result, but in this case you get a spew of XML because the command is not found... It might take you a while to realize that pwsh (or PowerShell, because this has always been like this) has produced all this output on stderr, and the hosting instance doesn't deserialize stderr output, even if it does have the CLIXML header.

    NOTE: You can work around this by redirecting the other stream to stdout ... but then it's all co-mingled...

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions