解析XML节点值以导出到CSV

时间:2020-03-04 14:36:31

标签: xml powershell

我正在尝试获取“ location path =”,“ allowUnlisted”以及所有IP地址的值,以便将其导出到csv。

XML示例

<configuration> 
 <location path="Example/5192_proxy">
        <system.webServer>
            <security>
                <ipSecurity allowUnlisted="false">
                    <add ipAddress="10.10.100.0" subnetMask="255.255.252.0" allowed="true" />
                    <add ipAddress="10.10.48.0" subnetMask="255.255.240.0" allowed="true" />
                    <add ipAddress="10.10.100.0" subnetMask="255.255.252.0" allowed="true" />
                    <add ipAddress="192.168.63.97" subnetMask="255.255.255.224" allowed="true" />
                </ipSecurity>
            </security>
        </system.webServer>
    </location>
    <location path="Example/3796_Proxy">
        <system.webServer>
            <security>
                <ipSecurity allowUnlisted="false">
                    <add ipAddress="192.168.30.52" allowed="true" />
                    <add ipAddress="10.10.48.0" subnetMask="255.255.240.0" allowed="true" />
                </ipSecurity>   
               </security>
        </system.webServer>
    </location>
</configuration>

到目前为止,这是我在“ ExampleCLient”字段中抛出的多个数字

$snippet.configuration.location."system.WebServer".security.ipSecurity |select @{L="ExampleClient";E={$snippet.configuration.location.path}}, allowUnlisted, add -ExpandProperty add

最终我希望它读起来像

ExampleClient   AllowUnlisted  IPAddress
Example/5192       False       10.10.100.0
Example/5192       False       10.10.48.0
...
Example/3796_Proxy False       192.168.30.52

编辑:

这有点奏效,我想我明白了!

[xml]$xml = (Get-Content .\Sample.xml)
$finum = $($xml.configuration.location.path)
FOREACH ($fi in $finum) 
 {
  $xml.configuration.location."system.webServer".security.ipSecurity |select allowUnlisted, add -ExpandProperty add |select @{L="FI";E={$fi}}, allowunlisted, ipaddress, subnetmask
 }

2 个答案:

答案 0 :(得分:1)

您可以按照以下步骤进行操作:

[xml]$snippet = @"
<configuration> 
 <location path="Example/5192_proxy">
        <system.webServer>
            <security>
                <ipSecurity allowUnlisted="false">
                    <add ipAddress="10.10.100.0" subnetMask="255.255.252.0" allowed="true" />
                    <add ipAddress="10.10.48.0" subnetMask="255.255.240.0" allowed="true" />
                    <add ipAddress="10.10.100.0" subnetMask="255.255.252.0" allowed="true" />
                    <add ipAddress="192.168.63.97" subnetMask="255.255.255.224" allowed="true" />
                </ipSecurity>
            </security>
        </system.webServer>
    </location>
    <location path="Example/3796_Proxy">
        <system.webServer>
            <security>
                <ipSecurity allowUnlisted="false">
                    <add ipAddress="192.168.30.52" allowed="true" />
                    <add ipAddress="10.10.48.0" subnetMask="255.255.240.0" allowed="true" />
                </ipSecurity>   
               </security>
        </system.webServer>
    </location>
</configuration>
"@

$result = $snippet.configuration.location | ForEach-Object {
    $client        = $_.path
    $allowUnlisted = $_.'system.WebServer'.security.ipSecurity.allowUnlisted
    foreach ($ip in $_.'system.WebServer'.security.ipSecurity.add.ipAddress) {
        [PsCustomObject]@{
            ExampleClient = $client 
            AllowUnlisted = $allowUnlisted
            IPAddress     = $ip
        }
    }
}

# output on screen
$result

# output to CSV file
$result | Export-Csv -Path 'D:\config.csv' -NoTypeInformation

屏幕结果:

ExampleClient      AllowUnlisted IPAddress    
-------------      ------------- ---------    
Example/5192_proxy false         10.10.100.0  
Example/5192_proxy false         10.10.48.0   
Example/5192_proxy false         10.10.100.0  
Example/5192_proxy false         192.168.63.97
Example/3796_Proxy false         192.168.30.52
Example/3796_Proxy false         10.10.48.0


根据您的评论,您还希望包括子网掩码,内部的foreach循环需要稍作更改:

$result = $snippet.configuration.location | ForEach-Object {
    $client        = $_.path
    $allowUnlisted = $_.'system.WebServer'.security.ipSecurity.allowUnlisted
    foreach ($item in $_.'system.WebServer'.security.ipSecurity.add) {
        [PsCustomObject]@{
            ExampleClient = $client 
            AllowUnlisted = $allowUnlisted
            IPAddress     = $item.ipAddress
            SubnetMask    = $item.subnetMask
        }
    }
}

结果:

ExampleClient      AllowUnlisted IPAddress     SubnetMask     
-------------      ------------- ---------     ----------     
Example/5192_proxy false         10.10.100.0   255.255.252.0  
Example/5192_proxy false         10.10.48.0    255.255.240.0  
Example/5192_proxy false         10.10.100.0   255.255.252.0  
Example/5192_proxy false         192.168.63.97 255.255.255.224
Example/3796_Proxy false         192.168.30.52                
Example/3796_Proxy false         10.10.48.0    255.255.240.0

答案 1 :(得分:1)

这听起来像是an XPath query或3的工作!

在下面的示例中,我们:

  1. 找到文档中的任何<add ipAddress="...">个节点
    //add[@ipAddress]
  2. 从第一个<location>祖先
    ./ancestor::location
  3. 中选择“ ExampleClient”值
  4. 类似地,从第一个<ipSecurity>祖先中选择“ AllowUnlisted”值
    ./ancestor::ipSecurity
$ipNodes = $snippet.SelectNodes('//add[@ipAddress]')

$ipNodes |Select-Object @{Name='ExampleClient';E={$_.SelectSingleNode('./ancestor::location').path}},@{Name='AllowUnlisted';E={[bool]::Parse($_.SelectSingleNode('./ancestor::ipSecurity').allowUnlisted)}},ipAddress