简体   繁体   English

Powershell 脚本搜索 XML 子元素

[英]Powershell script to search an XML sub-elements

Hello I'm having problem trying to get the values of one node with certain name from XML.您好,我在尝试从 XML 获取具有特定名称的一个节点的值时遇到问题。

For example:例如:

<Smart>
  <Settings>
    <Service name="9003">
       <Config imports="router">
           <Section name="x">
                <Parameter name="a" value="0" />
                <Parameter name="b" value="1" />
                <Parameter name="c" value="2" />
                <Parameter name="d" value="3" />
                <Parameter name="e" value="4" />
           </Section>
        </Config>
    </Service>
    <Service name="9004">
       <Config imports="router">
           <Section name="x">
                <Parameter name="a" value="5" />
                <Parameter name="b" value="6" />
                <Parameter name="c" value="7" />
                <Parameter name="d" value="8" />
                <Parameter name="e" value="9" />
           </Section>
        </Config>
    </Service>
  </Settings>
</Smart>

I want to get the value '9' from the Parameter named as "e" from the Service "9004" and then export or print it using the Write-Host.我想从服务“9004”中名为“e”的参数中获取值“9”,然后使用 Write-Host 导出或打印它。

Any ideas?有任何想法吗? I was trying this but it is returning nothing to me.我正在尝试这个,但它没有给我任何回报。

    # Read the XML file
    Write-Host "OPENING XML FILE";
    $path = "\\$computer\$FileName"
    [xml] $xml = Get-Content $path
    
    # Filter the XML
    $SectionName = "x"
    $Section = $xml.Smart.Settings.Config.Section| Where-Object {$_.name -eq $SectionName}
    Write-Host $Section

@Brazs, try using XPath to select the element based on the path and name value, like so @Brazs,尝试使用 XPath 到 select 基于路径和名称值的元素,就像这样

$parameterElement = $xml.SelectSingleNode("//Section[@name='$SectionName']/Parameter[@name='e']")

You can get to the attribute value directly like this您可以像这样直接获取属性值

$parameterElement.value

or with a method或用一种方法

$parameterElement.GetAttribute('value')

EDIT: The search criteria can be expanded too, so编辑:搜索条件也可以扩展,所以

$parameterElement = $xml.SelectSingleNode("//Service[@name='$ServiceName']/Config/Section[@name='$SectionName']/Parameter[@name='e']")

finds the required service, any config, the required section and the 'e' Parameter.找到所需的服务、任何配置、所需的部分和“e”参数。 I leave it to you to figure out how to expand on that for a specific config or variable Parameter, for example.例如,我留给您弄清楚如何针对特定配置或变量参数进行扩展。

A bit of a different approach to allow you to dynamically cherry-pick as needed.一种不同的方法,可让您根据需要动态选择。

[xml]$XmlData = @'
<Smart>
  <Settings>
    <Config imports="alpha">
        <Section name="x">
           <Parameter name="a" value="true" />
           <Parameter name="b" value="0" />
           <Parameter name="c" value="13873" />
           <Parameter name="d" value="true" />
           <Parameter name="e" value="EAI" />
        </Section>
        <Section name="y">
           <Parameter name="a" value="false" />
           <Parameter name="b" value="1" />
           <Parameter name="c" value="13874" />
           <Parameter name="d" value="false" />
           <Parameter name="e" value="EAI1" />
        </Section>
    </Config>
  </Settings>
</Smart>
'@


($SectionData = Select-Xml -Xml $XmlData -XPath '//Section').Node 
# Results
<#
name Parameter      
---- ---------      
x    {a, b, c, d, e}
y    {a, b, c, d, e}
#>

$SectionData.Node.SyncRoot | 
Where-Object name -eq 'x' 
# Results
<#
name Parameter      
---- ---------      
x    {a, b, c, d, e}
#>


($SectionData.Node.SyncRoot | 
Where-Object name -eq 'x').Parameter | 
Where-Object name -eq 'e'
# Results
<#
name value
---- -----
e    EAI 
#>


(($SectionData.Node.SyncRoot | 
Where-Object name -eq 'x').Parameter | 
Where-Object name -eq 'e').Value
# Results
<#
EAI
#>

Using your numbers XMLData, then this:使用您的数字 XMLData,然后:

(((($SectionData.Node.SyncRoot | 
Where-Object name -eq '9003').Config).Section).Parameter | 
Where-Object -Property name -eq 'e').Value
# Results
<#
4
#>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM