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,710 points
903 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
• 58,020 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
1,025 views
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,311 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
1,101 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,975 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,468 views
0 votes
1 answer
+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,020 points
1,077 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
13,593 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