在 Powershell 中使用循环在 XML 文件中添加元素

using Loops to Add Element in XML File in Powershell

我有一个 XML 文件,我从 Powershell 上的以下代码中获取该文件,然后将其转换为 CSV 以在网格视图中获得所需的输出

    foreach($u in $global:Server) {
            $ret=(Invoke-SSHCommand -Index 0 -Command "mccli backup show --name=/clients/$u --verbose=true --xml | tee -a /tmp/backup.xml")
            
            } 
    Get-SCPFile -ComputerName "$global:Avamar" -Credential $LoginCredentials -RemoteFile "/tmp/backup.xml" -LocalFile 'C:\Temp\backup.xml'
        [xml]$log = "<logroot>$(Get-Content C:\Temp\backup.xml)</logroot>"
        $log.logroot.CLIOutput.Data.ChildNodes | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Set-Content "C:\Temp\new.csv"
Import-Csv -Path "C:\Temp\new.csv" | Out-GridView -Title Get-CsvData

backup.xml 文件如下所示

    <CLIOutput>
          <Results>
            <ReturnCode>0</ReturnCode>
            <EventCode>23000</EventCode>
            <EventSummary>CLI command completed successfully.</EventSummary>
          </Results>
          <Data>
            <Row>
              <Created>2022-05-06 09:17 PM</Created>
              <LabelNum>660</LabelNum>
              <Size>31.5 GB</Size>
              <Retention>D</Retention>
              <Hostname>abc.com</Hostname>
              <Location>Local</Location>
              <ConsistentLevel>not_available</ConsistentLevel>
              <Tier>Active</Tier>
              <Label>VSS_SNAP</Label>
              <Plugin>Windows VSS</Plugin>
              <Expires>2022-06-03 09:00 PM</Expires>
              <Server>def.com</Server>
            </Row>
            <Row>
              <Created>2022-05-06 09:06 PM</Created>
              <LabelNum>657</LabelNum>
              <Size>34.9 GB</Size>
              <Retention>D</Retention>
              <Hostname>abc.com</Hostname>
              <Location>Local</Location>
              <ConsistentLevel>not_available</ConsistentLevel>
              <Tier>Active</Tier>
              <Label>GSK_Windows2008</Label>
              <Plugin>Windows File System</Plugin>
              <Expires>2022-06-03 09:00 PM</Expires>
              <Server>def.com</Server>
            </Row>
            </Data>
        </CLIOutput>
        
        <CLIOutput>
          <Results>
            <ReturnCode>0</ReturnCode>
            <EventCode>23000</EventCode>
            <EventSummary>CLI command completed successfully.</EventSummary>
          </Results>
          <Data>
            <Row>
              <Created>2022-05-06 09:13 PM</Created>
              <LabelNum>1009</LabelNum>
              <Size>37.3 GB</Size>
              <Retention>D</Retention>
              <Hostname>abc.com</Hostname>
              <Location>Local</Location>
              <ConsistentLevel>not_available</ConsistentLevel>
              <Tier>Active</Tier>
              <Label>GSK_Windows2008</Label>
              <Plugin>Windows File System</Plugin>
              <Expires>2022-06-03 09:00 PM</Expires>
              <Server>def.com</Server>
            </Row>
            <Row>
              <Created>2022-05-06 09:08 PM</Created>
              <LabelNum>1008</LabelNum>
              <Size>38.2 GB</Size>
              <Retention>D</Retention>
              <Hostname>dredsavau1-01.bio.corpnet1.com</Hostname>
              <Location>Local</Location>
              <ConsistentLevel>not_available</ConsistentLevel>
              <Tier>Active</Tier>
              <Label>VSS_SNAP</Label>
              <Plugin>Windows VSS</Plugin>
              <Expires>2022-06-03 09:00 PM</Expires>
              <Server>def.com</Server>
            </Row>
            </Data>
        </CLIOutput>

XML

的预期输出
    <CLIOutput>
      <Results>
        <ReturnCode>0</ReturnCode>
        <EventCode>23000</EventCode>
        <EventSummary>CLI command completed successfully.</EventSummary>
      </Results>
      <Data>
        <Row>
          <Client>Sample1.com</Client>
          <Created>2022-05-06 09:17 PM</Created>
          <LabelNum>660</LabelNum>
          <Size>31.5 GB</Size>
          <Retention>D</Retention>
          <Hostname>abc.com</Hostname>
          <Location>Local</Location>
          <ConsistentLevel>not_available</ConsistentLevel>
          <Tier>Active</Tier>
          <Label>VSS_SNAP</Label>
          <Plugin>Windows VSS</Plugin>
          <Expires>2022-06-03 09:00 PM</Expires>
          <Server>def.com</Server>
        </Row>
        <Row>
          <Client>Sample1.com</Client>
          <Created>2022-05-06 09:06 PM</Created>
          <LabelNum>657</LabelNum>
          <Size>34.9 GB</Size>
          <Retention>D</Retention>
          <Hostname>abc.com</Hostname>
          <Location>Local</Location>
          <ConsistentLevel>not_available</ConsistentLevel>
          <Tier>Active</Tier>
          <Label>GSK_Windows2008</Label>
          <Plugin>Windows File System</Plugin>
          <Expires>2022-06-03 09:00 PM</Expires>
          <Server>def.com</Server>
        </Row>
        </Data>
    </CLIOutput>

    <CLIOutput>
      <Results>
        <ReturnCode>0</ReturnCode>
        <EventCode>23000</EventCode>
        <EventSummary>CLI command completed successfully.</EventSummary>
      </Results>
      <Data>
        <Row>
          <Client>Sample2.com</Client>
          <Created>2022-05-06 09:13 PM</Created>
          <LabelNum>1009</LabelNum>
          <Size>37.3 GB</Size>
          <Retention>D</Retention>
          <Hostname>abc.com</Hostname>
          <Location>Local</Location>
          <ConsistentLevel>not_available</ConsistentLevel>
          <Tier>Active</Tier>
          <Label>GSK_Windows2008</Label>
          <Plugin>Windows File System</Plugin>
          <Expires>2022-06-03 09:00 PM</Expires>
          <Server>def.com</Server>
        </Row>
        <Row>
          <Client>Sample2.com</Client>
          <Created>2022-05-06 09:08 PM</Created>
          <LabelNum>1008</LabelNum>
          <Size>38.2 GB</Size>
          <Retention>D</Retention>
          <Hostname>dredsavau1-01.bio.corpnet1.com</Hostname>
          <Location>Local</Location>
          <ConsistentLevel>not_available</ConsistentLevel>
          <Tier>Active</Tier>
          <Label>VSS_SNAP</Label>
          <Plugin>Windows VSS</Plugin>
          <Expires>2022-06-03 09:00 PM</Expires>
          <Server>def.com</Server>
        </Row>
        </Data>
    </CLIOutput>

<!-- end snippet -->

我想在 XML 输出文件中获取或附加客户端名称,以明确哪一行属于哪个客户端。

如果我对你的理解正确,就更新你的 xml 而言,你可能正在寻找这样的东西:

$xmlFragment=$log.CreateDocumentFragment()
$clients = 'Sample1.com','Sample2.com'    
$targets = $log.selectnodes('//Data')

for ($i = 0; $i -lt $targets.Count; $i++) {
    $rows = $targets[$i].selectnodes(".//Row")
    $item = $clients[$i]
    $new_node = "<client>$item</client>"    
    foreach ($row in $rows) {
        $xmlFragment.InnerXML=$new_node
        $row.PrependChild($xmlFragment)
    }
}