We help IT Professionals succeed at work.
Get Started

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

CuriousMAUser
on
59 Views
Last Modified: 2015-02-27
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
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2014
Commented:
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