我正在尝试获取“ 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
}
答案 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
$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的工作!
在下面的示例中,我们:
<add ipAddress="...">
个节点//add[@ipAddress]
<location>
祖先./ancestor::location
<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