PowerShell to get attribute values from XML with multiple attributes

Question:

The following XML file is one Object node of the output from the command Get-ClusterGroup run from a 2008 R2 Failover Cluster with PowerShell 2:

The full file has three more Object nodes similar to this. Two of those nodes have the value “False” in the “IsCoreGroup” attribute and the other two are “True”. What I am trying to do is get the value of the “Name” property and other attributes from the Object nodes that have the value of “False” in the “IsCoreGroup” attribute.

I have tried a number of ways to get this attribute but can’t figure out how to drill down into the sibling attributes.

Here’s what I have so far:

This gives me the following:

But I can’t figure out how to get the sibling properties

I tried backing up a level with:

But that’s not getting me anywhere.

Any help is greatly appreciated!

Answer:

You need to access the parentnode if you’re variable points at a property-element. Since you need to find a property element where the name is a attribute-value, I prefer using xpath to do this.

You could also have done $xmlproperty.parentnode.whateveryouwant.

Personally I’d use xpath to search for the right objects to begin with and retrieve them at object-level, so you can easily access the other properties in the object-node without going up a level.

Source:

PowerShell to get attribute values from XML with multiple attributes by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply