-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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