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

function Get-ServerInfo {
<#
.SYNOPSIS
Uses WMI to retrieve details from one or more servers.

.DESCRIPTION
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
Get-ServerInfo.

.EXAMPLE
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

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

.EXAMPLE
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
#>

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [Alias('name')]
        [string[]]$computername,
        [string]$logfile = 'failed.txt'
    )
    BEGIN {
        Del $logfile -ea SilentlyContinue
    }
    PROCESS {
        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 = @{
                    'ComputerName'=$computer;
                    'BIOSSerial'=$bios.serialnumber;
                    'OSVersion'=$os.caption;
                    'OSBuild'=$os.buildnumber;
                    'SPVersion'=$os.servicepackmajorversion;
                    'OSArch'=$os.osarchitecture;
                    'ProcArch'=$proc.addresswidth
                }
                $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
CuriousMAUserAsked:
Who is Participating?
 
footechCommented:
Just tested it and it's working fine.  As long as your -searchbase is correct so that Get-ADComputer is returning results then you should be fine.
0
 
footechCommented:
Not sure what you're asking.  The last line of this already outputs to .CSV.
Import-Module ActiveDirectory
 Get-ADComputer -filter * `
   -searchbase 'ou=servers,dc=company,dc=com' |
   Select -expand Name | Get-ServerInfo | Export-csv C:\Scripts\ServerDetails.csv 

Open in new window

The function Get-ServerInfo outputs objects, after that you can handle the object however you'd like.
0
 
CuriousMAUserAuthor Commented:
I understand. The output is empty. I'm trying to output the servers os architecture information to prepare to migrate necessary 2003 servers to 2008 R2. Something appears out of sink. Any thoughts?

 'ComputerName'=$computer;
                    'BIOSSerial'=$bios.serialnumber;
                    'OSVersion'=$os.caption;
                    'OSBuild'=$os.buildnumber;
                    'SPVersion'=$os.servicepackmajorversion;
                    'OSArch'=$os.osarchitecture;
                    'ProcArch'=$proc.addresswidth
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.