We help IT Professionals succeed at work.
Get Started

How do I output the function call to a CSV file for review?

Last Modified: 2015-02-27
function Get-ServerInfo {
Uses WMI to retrieve details from one or more servers.

Get-ServerInfo retrieves several pieces of information from one
or more remote servers.

.PARAMETER computername
One or more computer names to query. Accepts pipeline input.

.PARAMETER logfile
Defaults to failed.txt; will contain the names of computers
that have failed. This file is cleared each time you run

This example illustrates how to read a file containing one
computer name per line, and then format the results as a table.
Get-Content names.txt | Get-ServerInfo | Format-Table

This example shows how to query just one computer.
Get-ServerInfo -computername SERVER-R2

This example queries all computers in the SERVERS OU of
the AD domain Company.Com

Import-Module ActiveDirectory
Get-ADComputer -filter * `
  -searchbase 'ou=servers,dc=sleepmed,dc=md' |
  Select -expand Name | Get-ServerInfo

        [string]$logfile = 'failed.txt'
    BEGIN {
        Del $logfile -ea SilentlyContinue
        foreach ($computer in $computername) {
            $continue = $true
            try {
                $os = Get-WmiObject -class Win32_OperatingSystem `
                 -computer $computer -ea Stop
            } catch {
                $computer | out-file $logfile -append
                $continue = $false
            if ($continue) {
                $bios = Get-WmiObject -class Win32_BIOS `
                 -computername $computer
                $proc = Get-WmiObject -class Win32_Processor `
                 -computername $computer | Select -first 1
                $hash = @{
                $obj = New-Object -TypeName PSObject -Property $hash
                Write-Output $obj

Import-Module ActiveDirectory
Get-ADComputer -filter * `
  -searchbase 'ou=servers,dc=company,dc=com' |
  Select -expand Name | Get-ServerInfo | Export-csv C:\Scripts\ServerDetails.csv
Watch Question
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE