Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

PowerShell - Parsing XML & Output to Excel

$
0
0

I'm making a script that will parse through 2,000 XML files for data & then output it to an Excel file. Here's what I have so far:

#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=14


$xl.Visible=$True

#Rows & Cols
$row=3
$col=1
$A=4
$B=4
$C=4
$D=1
$E=2
$F=3
$G=9


#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 "}",""
$cells.EntireColumn.AutoFit()
$B++;$E++

}

But when I run it, the data comes out wrong:

I can add an XML if needed.



Viewing all articles
Browse latest Browse all 2562

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>