PowerShell retrieves attribute values โ€‹โ€‹from XML with multiple attributes

The following XML file is a single output node object from the Get-ClusterGroup command that is launched from a 2008 R2 failover cluster using PowerShell 2:

<?xml version="1.0"?> <Objects> <Object> <Property Name="Cluster">Cluster1</Property> <Property Name="IsCoreGroup">False</Property> <Property Name="OwnerNode">Node1</Property> <Property Name="State">Offline</Property> <Property Name="Name">SAP PL1</Property> <Property Name="Description" /> <Property Name="PersistentState">1</Property> <Property Name="FailoverThreshold">4294967295</Property> <Property Name="FailoverPeriod">6</Property> <Property Name="AutoFailbackType">1</Property> <Property Name="FailbackWindowStart">4294967295</Property> <Property Name="FailbackWindowEnd">4294967295</Property> <Property Name="Priority">1</Property> <Property Name="DefaultOwner">4294967295</Property> <Property Name="AntiAffinityClassNames" /> <Property Name="Id">a5ff557f-c81a-43aa-bdb9-e09d0a1103df</Property> </Object> </Objects> 

There are three more Object nodes in the complete file, similar to this. Two of these nodes are set to False in the IsCoreGroup attribute, and the other two are True. What I'm trying to do is get the value of the "Name" property and other attributes from the Object nodes that are set to "False" in the "IsCoreGroup" attribute.

I tried several ways to get this attribute, but can't figure out how to expand the sibling attributes.

Here is what I still have:

 [xml]$file = get-content C:\Admin\ClusterGroups.xml $xmlProperties = $file.SelectNodes("/Objects/Object/Property") Foreach ($xmlProperty in $xmlProperties) { $strName = ($xmlProperty | Where-Object {$_.Name -eq "IsCoreGroup" }).InnerXml If ($strName -eq "False") { Echo $xmlProperty } } 

This gives me the following:

 Name #text ---- ----- IsCoreGroup False 

But I can't figure out how to get sibling properties

I tried to backup using:

 [xml]$file = get-content C:\Admin\ClusterGroups.xml $xmlObjects = $file.SelectNodes("/Objects/Object") Foreach ($xmlObject in $xmlObjects) { $strCoreGroup = ($xmlObject | Where-Object {$_.Property.Name -eq "IsCoreGroup" }).InnerXml If ($strCoreGroup -eq "False") { Echo $xmlObject } } 

But it wonโ€™t deliver me.

Any help is much appreciated!

+6
source share
1 answer

You need to access the parent path if you are variable points in the property element. Since you need to find a property element where the name is the value of the attribute, I prefer using xpath for this.

 $xmlProperties = $file.SelectNodes("/Objects/Object/Property") Foreach ($xmlProperty in $xmlProperties) { $strName = ($xmlProperty | Where-Object {$_.Name -eq "IsCoreGroup" }).InnerXml If ($strName -eq "False") { # .. means parent node. So the xpath goes up one level from property, and searches for the new property you want. $xmlProperty.SelectSingleNode('../Property[@Name="Name"]').InnerXml } } 

You could also do $xmlproperty.parentnode.whateveryouwant .

Personally, I would use xpath to find the right objects to get started and get them at the object level so that you can easily access the other properties of the node object without going up to the level.

 $file.SelectNodes('/Objects/Object[Property[@Name="IsCoreGroup"]="False"]') | % { #Foreach object with IsCoreGroup = false, get value of property with Cluster1 as Name attribute $_.SelectSingleNode('Property[@Name="Cluster"]').innerxml } Cluster1 
+5
source

Source: https://habr.com/ru/post/957376/


All Articles