解析多个.XML文件以获取节点值

时间:2020-03-04 19:17:35

标签: xml powershell

尝试读取多个xml文件并收集多个节点的值。

$ configlist是一个txt文件,在UNC路径中包含多个xml文件

\\server\path\to\cml.config
\\otherserver\path\to\xml.config

如果我尝试将UNC路径之一作为一个,在foreach之外(即[xml] $ xmlfull =(Get-Content“ \ uncpath \ to \ xml.config”)),它可以工作,如果我尝试用foreach遍历列表,什么也没做。

如何从$ configlist中使用多个XML文件呢?

$configlist= (get-content C:\temp\cfgpath_tested.txt)
Foreach ($cfg in $configlist)
{ 
[xml]$xmlfull = (Get-Content "$cfg")
$result = $xmlfull.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
      } 
    } 
  }
}

# output on screen
$result 

1 个答案:

答案 0 :(得分:1)

在第一个循环中捕获结果:

$result = foreach ($cfg in $configlist)

然后从$result =行中删除$result = $xmlfull.configuration.location

对不起,这次是在移动设备上的简短回答。