Solved

Powershell Select from xml

Posted on 2013-06-22
4
243 Views
Last Modified: 2013-08-31
I need to be able to select from an xml based on a value then parse for specific child nodes in that parent and perform specific functions:

Here is the xml snippet:
<ServerList>
  <Server Name="SERVER01">
    <IP Address="192.16.1.1" Status="In Service" Virtual="8.8.8.55" />
    <IP Address="192.16.1.2" Status="In Service" Virtual="8.8.8.66" />
    <IP Address="192.16.1.3" Status="In Service" Virtual="8.8.8.77" />
  </Server>

  <Server Name="SERVER02">
    <IP Address="192.16.1.19" Status="In Service" Virtual="8.8.8.55" />
    <IP Address="192.16.1.14" Status="In Service" Virtual="8.8.8.66" />
    <IP Address="192.16.1.17" Status="In Service" Virtual="8.8.8.77" />
  </Server>

  <Server Name="SERVER03">
    <IP Address="192.16.1.44" Status="In Service" Virtual="8.8.8.55" />
    <IP Address="192.16.1.48" Status="In Service" Virtual="8.8.8.66" />
    <IP Address="192.16.1.56" Status="In Service" Virtual="8.8.8.77" />
  </Server>

  <Server Name="SERVER04">
    <IP Address="192.16.1.78" Status="In Service" Virtual="8.8.8.55" />
    <IP Address="192.16.1.68" Status="In Service" Virtual="8.8.8.66" />
    <IP Address="192.16.1.97" Status="In Service" Virtual="8.8.8.77" />
  </Server>
</ServerList>

Open in new window


Now I do not have any control over the xml formatting so unless someone is willing to do the heavy lifting to change the xml that is all I have to work with.

I want to be able to do something similar to below:
Select * from xml where server = "server01"

Then I want to query for all the virtuals for that machine then do a foreach loop:
e.g

foreach ($virtual in$virtuals)

{ do something}


Let me know if anyone thinks they can help :)
0
Comment
Question by:becraig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 29

Author Comment

by:becraig
ID: 39268437
Ok I got this figured out, thanks a lot guys
0
 
LVL 8

Expert Comment

by:dicconb
ID: 39407680
Good news :)

Would you consider posting the solution so other people can benefit from it?

D
0
 
LVL 29

Accepted Solution

by:
becraig earned 0 total points
ID: 39440340
Sure thing:
$var = Select-Xml -Path "path-to-file.xml" -XPath "/ServerList/Server/IP" | where { $_.Node.ParentNode.Name -eq "servername"} | % {$_.Node.Virtual} | select -unique 

Open in new window

0
 
LVL 29

Author Closing Comment

by:becraig
ID: 39454739
The key was in using _.Node.ParentNode to specify the parent node of the node I was specifying in my query.
0

Featured Post

Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question