PowerShell 从具有多个属性的 XML 中获取属性值 [英] PowerShell to get attribute values from XML with multiple attributes

查看:62
本文介绍了PowerShell 从具有多个属性的 XML 中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 XML 文件是使用 PowerShell 2 从 2008 R2 故障转移群集运行的命令 Get-ClusterGroup 输出的一个对象节点:

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:

<?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>

完整的文件还有三个与此类似的对象节点.其中两个节点在IsCoreGroup"属性中的值为False",另外两个为True".我想要做的是从IsCoreGroup"属性中具有False"值的对象节点获取Name"属性和其他属性的值.

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.

这是我目前所拥有的:

[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
    }
}

这给了我以下内容:

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

但是我不知道如何获得兄弟属性

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

我尝试使用以下方法备份关卡:

I tried backing up a level with:

[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 that's not getting me anywhere.

非常感谢任何帮助!

推荐答案

如果您是属性元素的变量点,则需要访问父节点.由于您需要查找名称为属性值的属性元素,因此我更喜欢使用 xpath 来执行此操作.

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.

$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
    }
}

你也可以做$xmlproperty.parentnode.whateveryouwant.

就我个人而言,我会使用 xpath 来搜索正确的对象并在对象级别检索它们,这样您就可以轻松访问对象节点中的其他属性,而无需上一级.

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.

$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

这篇关于PowerShell 从具有多个属性的 XML 中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆