Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
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 2:21
    
please post that as an answer and mark it as accepted. –  jscott Jun 29 '13 at 17:00

3 Answers 3

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

Example:

Get-Process | Export-Csv process.txt
share|improve this answer
    
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 1:44

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
share|improve this answer

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.