Sune
Sune

Reputation: 3280

Outputting "foreach" results to CSV (with columns intact) (listing all services on all servers in AD)

Using the following code:

   # List all servers
    $servers = Get-QADComputer -OSName *server*,*hyper-v*

    # Looping through the servers
    foreach ($server in $servers) {
    $i++
    $intSize = $intSize + $server.Length

    # Writing statusbar
    Write-Progress -activity "Scanning servers . . ." -status "Scanned: $i of $($servers.length) - Working on $($server.name)" -percentComplete (($i / $servers.length)  * 100)

    # Pinging the server in Powershell-way!
    if (Test-Connection -ComputerName $server.name -count 1 -Quiet ) {
        echo $server.name
    }
}
$respondingservers = $i - $notresponding

I am looping through my list of my 270 AD servers and counting who is online, running snmp, wmi etc. But what I would like to do is NOT specify what services to check for and get a complete list on every server. I'd like to output this to CSV so I can import it to Excel and sort on the name of the service etc. This of course means that it needs to be output in a sensible manner.

For example, if server X is running DNS SERVER and it ends up in column 4, all of the following DNS SERVER results should end up in the same column in my spreadsheet.

Am I asking for too much or can it be done? Right now I am not even able to output to CSV so any help would be gratefully appreciated.

Upvotes: 1

Views: 5828

Answers (1)

jon Z
jon Z

Reputation: 16646

You could export services information for your servers like this:

    # Pinging the server in Powershell-way!
    if (Test-Connection -ComputerName $server.name -count 1 -Quiet ) {
        echo $server.name
        $services = $services, (get-wmiobject -ComputerName $server.name win32_service | select systemname, name, started)
    }
}
# export information about services to csv file
$services | Export-Csv services.csv -NoTypeInformation

This would produce a csv file with the following structure:

"systemname","name","started"
"SERVER01","AeLookupSvc","False"
"SERVER02","AeLookupSvc","False"
...

This csv can then be used to create a pivot table in Excel (rows = systemname, columns = name) to display the information you want.

Upvotes: 2

Related Questions