以下 XML 文件是使用 PowerShell 2 从 2008 R2 故障转移集群运行命令 Get-ClusterGroup 的输出的一个对象节点:
<?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”属性和其他属性的值。
我已经尝试了多种方法来获取此属性,但无法弄清楚如何向下钻取同级属性。
这是我目前所拥有的:
[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
但我不知道如何获取兄弟属性
我尝试备份关卡:
[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
}
}
但这对我没有任何帮助。
非常感谢任何帮助!
最佳答案
如果您是属性元素的变量点,则需要访问父节点。由于您需要找到名称为属性值的属性元素,因此我更喜欢使用 xpath 来执行此操作。
$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 来搜索正确的对象,并在对象级别检索它们,这样您就可以轻松访问对象节点中的其他属性,而无需提升级别。
$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
关于xml - PowerShell 从具有多个属性的 XML 中获取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776186/