PowerShell: Retrieve all “live” (pingable) server names, manufacturers, models & serial number

This script searches AD for any server running Windows Server, strips the names out to make a list, pings the list to avoid wasting time and either gathers some WMI data or reports that the server is offline. It then collates the data for individual servers into one line which it writes to a CSV.

My next script is basically the same thing, but it filters out Dell servers at the start.
=======================================
Remove-Item .\allServers_Name_ST_Model.csv #deletes the old export file
[string]$serverList #this will contain the details of any device running Windows Server in AD
[string]$serverNames
[string]$serverArray #this will contain the name, manufacturer, model and serial number in an array
[int]$xAxis #keeeps track of the vertical array elements
[int]$yAxis #keeps track of the horizontal array elements
$serverList = Get-ADComputer -LDAPFilter "(&(ObjectCategory=Computer)(OperatingSystem=*server*))" | Select-Object Name | Sort-Object Name #Pulls any Windows Servers out of AD
$serverNames = $serverList.Name #Extracts just server names out of $serverList
$serverArray = New-Object 'object[,]' $serverList.Count,4 #creates the array with the same number of lines as there are servers in $serverList, and 4 columns-
#name, manufacturer, model, serial number.
$xAxis = 0 #arrays start at 0!
$yAxis = 0 #arrays start at 0!
ForEach ($server in $serverNames) #starts the loop, cycles through each server in turn
{
$isAlive = Test-Connection $Server -Count 1 -Quiet #creates a variable that will return true/ false depending on wther the server responds
 If($isAlive -eq $true) #if the server responds... pull WMI data from different classes, -ExpandProperty makes the output a bit prettier
 {
 Write-Host "Server $server is fine" #this isn't really necessary, it's handy for manual testing
 $serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server | Select-Object CSName -ExpandProperty CSName
 $yAxis++ #increments $yAxis fro 0 > 1 to access next array element
 $serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Manufacturer -ExpandProperty Manufacturer
 $yAxis++ #increments $yAxis fro 1 > 2 to access next array element
 $serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Model -ExpandProperty Model
 $yAxis++ #increments $yAxis fro 2 > 3 to access next array element (remember 3 is the last column in a 4-column array)
 $serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_SystemEnclosure -ComputerName $server | Select-Object SerialNumber -ExpandProperty SerialNumber
 }
 ElseIf($isAlive -eq $false) #if the server doesn't respond, write the server name with "is not responding" to the array
 {
 $serverArray[$xAxis,$yAxis] = $server
 $yAxis++
 $serverArray[$xAxis,$yAxis] = "is not responding"
 }
 $yAxis=0 #resets the $yAxis so it begins at 0 on the next line
 $xAxis++ #increments $xAxis, this never gets reset to 0 as you just want it to cycle through each server until the end
}

#The $serverArray variable is fully populated by now, so we need to make the data suitable for output
$yAxis=0
$xAxis=0
ForEach($item in $serverArray) #loops through each cell of the array in order to put the collections into comma-separated lines of text
{
 $gatherServerArrayContent = $csName = $serverArray[$xAxis,$yAxis]
 $gatherServerArrayContent += ","
 $gatherServerArrayContent += $manufacturer = $serverArray[$xAxis,($yAxis+1)]
 $gatherServerArrayContent += ","
 $gatherServerArrayContent += $model = $serverArray[$xAxis,($yAxis+2)]
 $gatherServerArrayContent += ","
 $gatherServerArrayContent += $serialNumber = $serverArray[$xAxis,($yAxis+3)]
$gatherServerArrayContent | Out-File .\allServers_Name_ST_Model.csv -Append #each server has it's own line in this file containing the 4 attributes
$yAxis = 0 #same again- reset the $yAxis for the next line
 $xAxis++ #same again- $xAxis never gets reset as it just has to cycle through the whole array
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s