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.
CodePudding user response:
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)
}
}