1

I have an XML file which I get from the below code on the Powershell and then I convert it to CSV to get the desired output in Grid view

    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 file looks like as below

    <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>

Expected Output of 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 -->

I want to get or append the Client name in the XML output file to make it clear what row belongs to which client.

2
  • It's a bit unclear from your question - do you want XML or CSV output? Commented May 9, 2022 at 20:07
  • I have updated my question, I want to get the element "<Client>Sample1.com</Client>" added to every row in the XML output file. Commented May 9, 2022 at 20:27

1 Answer 1

1

If I understand you correctly, in terms of updating your xml, you are probably looking for something like this:

$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)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Minor nitpick - avoid generating xml text in $new_node - in a pathological edge-case where, say, $item is something like <aaa /> or <<< aaa you'll get unexpected results. Try this in your foreach instead: $node = $row.OwnerDocument.CreateElement("client"); $node.InnerText = $item; $row.PrependChild($node); - the client name will be properly HTML-escaped then...
@mclayton Well, I'll be damned! I wasn't aware of the problems with those edge cases, but I tried your approach and it certainly works. As they say, live and learn!
Thanks @JackFleeting for understanding my point and helping with the perfect solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.