Reading powershell clixml in python

0 votes

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.

Oct 9, 2018 in Python by eatcodesleeprepeat
• 4,720 points
86 views

1 answer to this question.

0 votes

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

answered Oct 9, 2018 by Priyaj
• 57,300 points

Related Questions In Python

0 votes
1 answer

Reading powershell clixml in python

PowerShell's Extended Type System supports auxiliary properties ...READ MORE

answered Sep 21, 2018 in Python by charlie_brown
• 7,720 points
126 views
0 votes
1 answer

Reading Unix Timestamp in Python

import datetime print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S')) That is the cleanest way ...READ MORE

answered May 15, 2018 in Python by Nietzsche's daemon
• 4,260 points
106 views
0 votes
1 answer

Error reading csv file in python

You are missing the extension. Try this: import ...READ MORE

answered Mar 28 in Python by Shri
119 views
0 votes
1 answer

Reading server response from requests module in Python.

You can read the response using the text option ...READ MORE

answered May 13 in Python by Kunal
29 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 423 views
0 votes
1 answer

Reading powershell clixml in python

PowerShell's Extended Type System supports auxiliary properties ...READ MORE

answered Oct 5, 2018 in Python by Priyaj
• 57,300 points
78 views
+1 vote
1 answer

Reading a large file, line by line in Python

The correct, fully Pythonic way to read ...READ MORE

answered Aug 21, 2018 in Python by Priyaj
• 57,300 points
69 views