MichaelB
MichaelB

Reputation: 1132

Reading powershell clixml in python

I have an XML file that was written from a powershell "Export-Clixml" call and it appears quite strange to me.

Here's a very simple sample:

PS C:\> Get-Date | Export-Clixml sample.xml
PS C:\> cat .\sample.xml
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <DT>2018-03-21T08:05:39.5085956-04:00</DT>
    <MS>
      <Obj N="DisplayHint" RefId="1">
        <TN RefId="0">
          <T>Microsoft.PowerShell.Commands.DisplayHintType</T>
          <T>System.Enum</T>
          <T>System.ValueType</T>
          <T>System.Object</T>
        </TN>
        <ToString>DateTime</ToString>
        <I32>2</I32>
      </Obj>
    </MS>
  </Obj>
</Objs>

How are you supposed to parse these "Objs" and "MS" and "TN" and the rest of it? I need something that can reliably read this in python. Does anyone have a way to do this?

ConvertTo-Xml has an output like this

<?xml version="1.0"?>
<Objects>
  <Object Type="System.Collections.Hashtable">
    <Property Name="Key" Type="System.String">someKey</Property>
    <Property Name="Value" Type="System.String">value</Property>
    <Property Name="Key" Type="System.String">otherKey</Property>
    <Property Name="Value" Type="System.String">otherValue</Property>
  </Object>
</Objects>

So there's no way to tell which "key" goes with which "value" other than the position.

Upvotes: 1

Views: 1034

Answers (2)

Dave
Dave

Reputation: 364

Can you regenerate the file? If so use ConvertTo-Xml rather than Export-CliXml

$Xml = Get-Date | ConvertTo-Xml
$Xml.Save("C:\Temp\File.xml")

File content:

<?xml version="1.0" encoding="utf-8"?>
<Objects>
  <Object Type="System.DateTime">21/03/2018 15:25:33</Object>
</Objects>

Convert hash table to object before export/save

$HashTable =
@{
    Prop1 = "Value1"
    Prop2 = "Value2"
    Prop3 = "Value2"
}

$Object = New-Object -TypeName PsCustomObject -Property $HashTable


$Xml = $Object | ConvertTo-Xml
$Xml.Save("C:\Temp\File.xml")

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174730

PowerShell's Extended Type System supports auxiliary properties and type information, and the XML serialization scheme thus needs some way of representing this metadata. In your case, everything under MS contains properties native to the MonadShell (the original moniker for PowerShell). TN is likely short for TypeNames.

The full schema you're looking for is available here if you want to write a parser/deserializer in python

Upvotes: 2

Related Questions