Skip to main content

XML Ausgabe formatieren (PrettyPrint)

Nutzt man in Powershell die Funtion ConvertTo-Xml ist das Ergebnis ein unlesbarer, chaotischer Block.

Beispiel, für den Inhalt des TEMP Ordners:

(dir $env:temp | select name -ExpandProperty name| convertto-xml).OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">acrocef_low</Object><Object Type="System.String">Citrix</Object><Object Type="System.String">FI-JRE</Object><Object Type="System.String">hsperfdata_j514890</Object><Object Type="System.String">Low</Object><Object Type="System.String">McAfeeLogs</Object><Object Type="System.String">Mozilla Firefox</Object><Object Type="System.String">outlook logging</Object><Object Type="System.String">winstone.tmp</Object><Object Type="System.String">WPF</Object><Object Type="System.String">AppV5_Refresh.ps1</Object><Object Type="System.String">client_config_information.log</Object><Object Type="System.String">client_config_office_information.log</Object><Object Type="System.String">ExchangePerflog_8484fa31a669569fcfcccd43.dat</Object><Object Type="System.String">FI</Object><Object Type="System.String">pcswinlg.log</Object><Object Type="System.String">publish5.aspx</Object><Object Type="System.String">TWAIN.LOG</Object><Object Type="System.String">Twain001.Mtx</Object><Object Type="System.String">Twunk001.MTX</Object><Object Type="System.String">Twunk002.MTX</Object><Object Type="System.String">{3C91865B-2F59-434A-8E45-CF42B74D619E} - OProcSessId.dat</Object><Object Type="System.String">{650FA22F-3F5F-4F50-A3C9-502639DB9D5A} - OProcSessId.dat</Object><Object Type="System.String">{9284B91E-5214-4349-8A39-62E6E399D230} - OProcSessId.dat</Object><Object Type="System.String">~DFA58C8CB778F43FA5.TMP</Object></Objects>


Nutzt man jedoch diese Funktion

Format-Xml
function Format-Xml {
<#
.SYNOPSIS
Format the incoming object as the text of an XML document.
#>
    param(
        ## Text of an XML document.
        [Parameter(ValueFromPipeline = $true)]
        [string[]]$Text
    )
 
    begin {
        $data = New-Object System.Collections.ArrayList
    }
    process {
        [void] $data.Add($Text -join "`n")
    }
    end {
        $doc=New-Object System.XML.XMLDocument
        $doc.LoadXml($data -join "`n")
        $sw=New-Object System.Io.Stringwriter
        $writer=New-Object System.Xml.XmlTextWriter($sw)
#       $writer.IndentChar="`t"
        $writer.Formatting = [System.Xml.Formatting]::Indented
        $doc.WriteContentTo($writer)
        $sw.ToString()
    }
}


Sieht das Ergebnis gleich viel Ansprechender aus


(dir $env:temp | select name -ExpandProperty name| convertto-xml).OuterXml  | Format-Xml
<?xml version="1.0" encoding="utf-8"?>
<Objects>
  <Object Type="System.String">acrocef_low</Object>
  <Object Type="System.String">Citrix</Object>
  <Object Type="System.String">FI-JRE</Object>
  <Object Type="System.String">hsperfdata_j514890</Object>
  <Object Type="System.String">Low</Object>
  <Object Type="System.String">McAfeeLogs</Object>
  <Object Type="System.String">Mozilla Firefox</Object>
  <Object Type="System.String">outlook logging</Object>
  <Object Type="System.String">winstone.tmp</Object>
  <Object Type="System.String">WPF</Object>
  <Object Type="System.String">AppV5_Refresh.ps1</Object>
  <Object Type="System.String">client_config_information.log</Object>
  <Object Type="System.String">client_config_office_information.log</Object>
  <Object Type="System.String">ExchangePerflog_8484fa31a669569fcfcccd43.dat</Object>
  <Object Type="System.String">FI</Object>
  <Object Type="System.String">pcswinlg.log</Object>
  <Object Type="System.String">publish5.aspx</Object>
  <Object Type="System.String">TWAIN.LOG</Object>
  <Object Type="System.String">Twain001.Mtx</Object>
  <Object Type="System.String">Twunk001.MTX</Object>
  <Object Type="System.String">Twunk002.MTX</Object>
  <Object Type="System.String">{3C91865B-2F59-434A-8E45-CF42B74D619E} - OProcSessId.dat</Object>
  <Object Type="System.String">{650FA22F-3F5F-4F50-A3C9-502639DB9D5A} - OProcSessId.dat</Object>
  <Object Type="System.String">{9284B91E-5214-4349-8A39-62E6E399D230} - OProcSessId.dat</Object>
  <Object Type="System.String">~DFA58C8CB778F43FA5.TMP</Object>
</Objects>