0

What is the best way of executing a function and exporting the results to csv (file needs to be timestamped)?

I am currently producing a CSV which does not contain the correct details in the correct format.

function Print-Output() {
    $outputString = "hostname,os,lob"

    $Applications.Keys | ForEach-Object {
        $outputString+=",$_"
    }

    $outputString

    $ComputerObjects | ForEach-Object {
        $outputString="$($_.hostname),$($_.os),$($_.lob)"
        $currComputerObject = $_

        $currComputerObject.apps.Keys | ForEach-Object {
            $outputString+=",$($currComputerObject.apps[$_])".ToLower()
        }
        $outputString
    }

    $outputString
}

Print-Output | export-csv "results.csv"
2
  • Is the problem that you don't have the right data, or you don't have the right format? IOW, you've only shown "this is what I do" - not "this is what I get, but this other example is what I expect." At first blush, it looks like you're building CSV strings, then calling Export-CSV which will basically double-CSV your data. Export-CSV takes a collection of collections and outputs that to a CSV format - your data shouldn't be in a CSV format before calling the cmdlet. Commented Oct 3, 2013 at 12:57
  • you should pipe objects to export-csv, not comma separated strings. Commented Oct 3, 2013 at 16:16

1 Answer 1

1

Try

$ComputerObjects | Export-csv "results.csv"

If you want to take a quick peek at the results, you can try

Start results.csv

In my environment, this starts up Excel, and tells it to load the contents of the CSV file into a worksheet. YMMV.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.