Reading XML files in PowerShell

In most cases I use plain old text files to provide data to simple PowerShell scripts.  Sometimes, however, I have to use more complex structured data.  In these situations I switch to XML, which PowerShell is very good at processing. 

Lets say I have a simple XML file called servers.xml:

<servers>
    <server>Server0001</server>
    <server>Server0002</server>
    <server>Server0003</server>
    <server>Server0004</server>
</servers>

Now lets say I want to get the servers from those files and execute a command on each.  First, I need to get the XML data from the file.  To do so I would execute something like this:

$xml = [xml](get-content servers.xml)

My $xml object now contains all the data I need to do the work.  To see the list of servers I would run the following command:

PS C:\Tools\Powershell> $xml.xaservers.server
Server0001
Server0002
Server0003
Server0004
PS C:\Tools\Powershell>

Now lets say that my data is broken into two groups as such:

<servers>
    <group1>
        <server>Server0001</server>
        <server>Server0002</server>
        <server>Server0003</server>
        <server>Server0004</server>
    </group1>
    <group2>
        <server>Server0005</server>
        <server>Server0006</server>
        <server>Server0007</server>
        <server>Server0008</server>
    </group2>
</servers>

No problem.  I just make sure I include the additional group level in my dot notation:

<servers>
    <group1>
        <server>Server0001</server>
        <server>Server0002</server>
        <server>Server0003</server>
        <server>Server0004</server>
    </group1>
    <group2>
        <server>Server0005</server>
        <server>Server0006</server>
        <server>Server0007</server>
        <server>Server0008</server>
    </group2>
</servers>

As you can see that the each node simply becomes a child of the $xml object, making it very easy to navigate to the data you need.  For example:

PS C:\Tools\Powershell> $xml = [xml](get-content data.xml)
PS C:\Tools\Powershell> $xml

servers
——-
servers

PS C:\Tools\Powershell> $xml.servers

group1                                                      group2
——                                                      ——
group1                                                      group2

PS C:\Tools\Powershell> $xml.servers.group1

server
——
{Server0001, Server0002, Server0003, Server0004}

PS C:\Tools\Powershell> $xml.servers.group2

server
——
{Server0005, Server0006, Server0007, Server0008}

PS C:\Tools\Powershell> $xml.servers.group1.server
Server0001
Server0002
Server0003
Server0004
PS C:\Tools\Powershell> $xml.servers.group2.server
Server0005
Server0006
Server0007
Server0008
PS C:\Tools\Powershell>

With this data in hand I can now run through the list using the ForEach-Object cmdlet. 

I’ll write another post on writing to XML files when I get a moment.

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: