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.

Sep 21, 2018 in Python by aryya
• 7,450 points
853 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 Sep 21, 2018 by charlie_brown
• 7,720 points

Related Questions In Python

0 votes
0 answers

Reading powershell clixml in python

I have an XML file that was ...READ MORE

Oct 5, 2018 in Python by eatcodesleeprepeat
• 4,710 points
1,064 views
0 votes
1 answer

Reading powershell clixml in python

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

answered Oct 9, 2018 in Python by Priyaj
• 58,090 points
706 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
786 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
• 58,090 points
715 views
0 votes
1 answer

Error reading csv file in python

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

answered Mar 28, 2019 in Python by Shri
2,640 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 14, 2019 in Python by Kunal
532 views
0 votes
1 answer

Error:UnicodeDecodeError when reading CSV file in Pandas with Python

Hello @kartik, Try this: import pandas as pd df = ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
7,415 views
0 votes
1 answer

Why is reading lines from stdin much slower in C++ than Python?

tl;dr: Because of different default settings in ...READ MORE

answered Dec 18, 2020 in Python by Nikita
1,008 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
12,940 views
0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,368 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP