我正在制作一个脚本,它将解析 2,000 个 XML 文件以获取数据,然后将其输出到 Excel 文件。这是我到目前为止所拥有的:
#Get XML File
$path = Get-ChildItem C:\Users\bcamareno\Desktop\xml\xml
#Start Excel
$xl=New-Object -ComObject "Excel.Application"
$wb=$xl.Workbooks.Add()
$ws=$wb.ActiveSheet
$cells=$ws.Cells
$cells.item(1,1)="HDD Report"
$cells.item(1,1).font.bold=$True
$cells.item(1,1).font.size=18
#Rows & Cols
$row=3
$col=1
$A=4
$B=4
$C=4
$D=1
$E=2
$F=3
#column headings
"Server","Disk0","Disk1","Disk2","Disk3","Disk4","Disk5","Disk6","Disk7" | foreach {
$cells.item($row,$col)=$_
$cells.item($row,$col).font.bold=$True
$col++
}
#Site Code
foreach ($item in $Path) {
$cells.item($A,$D)= $item -replace "_pdisk.xml",""
$A++
}
#Grab ProductID
foreach ($item in $Path) {
[xml]$content = Get-Content "c:\Users\bcamareno\Desktop\xml\xml\$item"
$try= $Content.SelectNodes('//ProductID') | select "#Text"
$cells.item($B,$E)= $try -replace "@{#text=","" -replace "}",""
$B,$E++
}
$xl.Visible=$True
但是当我运行它时,数据出现错误,似乎它只是解析最后一个文件然后将结果添加到单元格中。
如果需要,我可以添加 XML。