Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Looping a function and outputting results to csv

$
0
0

No, you can’t. Combining information from two places into a single output is more complex. If you want a usable CSV file, then you have to query the pieces you want, and then combine them into a unified custom output object.

Short example:

function foo ($computers) {
  foreach ($computer in $computers)
    $os = get-wmiobject win32_operatingsystem -computer $computer
    $cs = get-wmiobject win32_computersystem -computer $computer
    $props = @{'ComputerName'=$computer;
               'OSBuild'=$os.buildnumber;
               'Mfgr'=$cs.manufacturer;
               'SPVersion'=$os.servicepackmajorversion}
    New-Object -Type PSObject -Prop $props
  }
}

foo -computers 'one','two','three' | export-csv output.csv

Check out “Learn PowerShell Toolmaking in a Month of Lunches” (book; MoreLunches.com). It really tackles this exact topic in a lot more depth, with more complete examples. You can even just grab the sample code if you want from the Web site. I’d actually start with “Learn Windows PowerShell 3 in a Month of Lunches” to nail down some of the important core stuff, like how the pipeline works, and how you can tell when you can pipe something to a command and have it work or not.


Viewing all articles
Browse latest Browse all 13067

Trending Articles