我有一個 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 輸出檔案中獲取或附加客戶端名稱,以明確哪一行屬于哪個客戶端。
uj5u.com熱心網友回復:
如果我理解正確,在更新您的 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)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472131.html
