PowerShell 从具有多个属性的 XML 中获取属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19776186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerShell to get attribute values from XML with multiple attributes
提问by user888818
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 文件是使用 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>
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.
完整文件还有三个与此类似的 Object 节点。其中两个节点在“IsCoreGroup”属性中的值为“False”,另外两个为“True”。我想要做的是从“IsCoreGroup”属性中具有“False”值的对象节点获取“Name”属性和其他属性的值。
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:
这是我到目前为止所拥有的:
[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 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.
但这并没有让我去任何地方。
Any help is greatly appreciated!
任何帮助是极大的赞赏!
采纳答案by Frode F.
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.
如果您是属性元素的变量点,则需要访问父节点。由于您需要查找名称为属性值的属性元素,因此我更喜欢使用 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
}
}
You could also have done $xmlproperty.parentnode.whateveryouwant.
你也可以做到$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.
就我个人而言,我会使用 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

