0

I need to get some details out of hundreds of XML files that were written for reporting. The script below provides the correct results, but I want them in file, cvs, xls, or txt so I can provide a list of the database fields that are in use by these reports.

$get = get-childitem D:\Data\Desktop\Projects\Reports\Test -include *.xml -recurse 
ForEach ($xmlfile in $get)
{
$Report = $xmlfile
$Fields = [xml](Get-Content $Report)
$Fields.reportsettings.fieldlist.field | select @{ L = 'Description'; E = {$_.desc}},
                                                @{ L = 'Field Name'; E = {$_.criterion}},
                                                @{ L = 'Field ID'; E = {$_.id} } } 

Thank you very much for your time!

Meepster
  • 31
  • 1
  • 5
  • I figured it out. Here is the code that worked for me for future use. `$get = get-childitem D:\Data\Desktop\Projects\Reports\Test -include *.xml -recurse ForEach ($xmlfile in $get) { $Report = $xmlfile $Fields = [xml](Get-Content $Report) $data += $Fields.reportsettings.fieldlist.field | select @{ L = 'Description'; E = {$_.desc}}, @{ L = 'Field Name'; E = {$_.criterion}}, @{ L = 'Field ID'; E = {$_.id} } } $data | Export-Csv fields.csv -NoTypeInformation` – Meepster Jun 29 '13 at 02:21
  • please post that as an answer and mark it as accepted. – jscott Jun 29 '13 at 17:00

3 Answers3

1

Here's a little function called Out-ExcelReport. Just pipe anything to it, and it will open in Microsoft Excel - in case it is installed on your system:

function Out-ExcelReport
{
  param
  (
    $Path = "$env:temp\$(Get-Random).csv"
  )

  $Input | Export-Csv -Path $Path -Encoding UTF8 -NoTypeInformation -UseCulture
  Invoke-Item -Path $Path
}
Get-Process | Out-ExcelReport
Itai Ganot
  • 10,644
  • 29
  • 93
  • 146
1

The 4 best ways to dump objects (and lists of objects) to a file are:

  • CSV - ConvertTo-Csv or Export-Csv
  • HTML - ConvertTo-Html
  • JSON - ConvertTo-Json
  • XML - ConvertTo-Xml

Keep in mind that convertto-* will just take the object, serialize, and spit out text. So you would have to use out-file

$test = "I am a object"
$test | ConvertTo-Xml | out-file "C:\test.xml"
Chad Carisch
  • 146
  • 2
0

You should try some command builtin in PowerShell, like Export-csv, ConvertTo-XML, Out-File, Export-Clixml.

Example:

Get-Process | Export-Csv process.txt
cuonglm
  • 2,386
  • 2
  • 16
  • 20
  • Yes I've tried those unfortunately it only returns 1 line from the very first xml file. If I use those like that I'd need help looping it through all of the files to get the results I need. $data = $Fields.reportsettings.fieldlist.field | select @{ L = 'Description'; E = {$_.desc}}, @{ L = 'Field Name'; E = {$_.criterion}}, @{ L = 'Field ID'; E = {$_.id} } } $data | Export-Csv fields.csv – Meepster Jun 29 '13 at 01:44