Trawl AD for devices running Windows Server, pull out hardware details, list them in PowerShell window

This is pretty much identical to the other two scripts but it displays the output in PowerShell. It’s got a few bits of colouring to differentiate between servers that can/ can’t be ping’d.

—————————————————
[string]$serverList
[string]$serverNames
[string]$serverArray
[int]$xAxis
[int]$yAxis
[int]$responsiveCounter
[int]$unresponsiveCounter
[string]$detail00
[string]$detail01
[string]$detail02
[string]$detail03

$serverList = Get-ADComputer -LDAPFilter “(&(ObjectCategory=Computer)(OperatingSystem=*server*))” | Select-Object Name
$serverNames = $serverList.Name

$serverArray = New-Object ‘object[,]’ $serverList.Count,4
$xAxis = 0
$yAxis = 0
[int]$responsiveCounter = 0
[int]$unresponsiveCounter = 0

Clear-Host

ForEach ($server in $serverNames)
{
$isAlive = Test-Connection $server -Count 1 -Quiet

If($isAlive -eq $true)
{
$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server | Select-Object CSName -ExpandProperty CSName
$detail00 = $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Manufacturer -ExpandProperty Manufacturer
$detail01 = $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Model -ExpandProperty Model
$detail02 = $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_SystemEnclosure -ComputerName $server | Select-Object SerialNumber -ExpandProperty SerialNumber
$detail03 = $serverArray[$xAxis,$yAxis]

$userInterface.ForegroundColor = “DarkGreen”
$userInterface.BackgroundColor = “DarkRed”

“{0, -20}”,”{1, -25}”,”{2, -20}”,”{3, -40}” -f $detail00, $detail01, $detail02, $detail03

$responsiveCounter++

}
ElseIf($isAlive -eq $false)
{
$userInterface.ForegroundColor = “Blue”
$userInterface.BackgroundColor = “Yellow”

$mergeDetails = “`n” + $server + ” is not responding so I can’t retrieve any data” + “`n”
$mergeDetails

$unresponsiveCounter++
}

$yAxis=0
$xAxis++
}

$userInterface.ForegroundColor = “DarkGreen”
$userInterface.BackgroundColor = “DarkRed”
Write-Host “`n`nThe total number of responsive servers is ” $responsiveCounter

$userInterface.ForegroundColor = “Blue”
$userInterface.BackgroundColor = “Yellow”
Write-Host “The total number of unresponsive servers is ” $unresponsiveCounter

$userInterface.BackgroundColor = “DarkGreen”
$userInterface.ForegroundColor = “Cyan”

Trawl AD for devices running Windows Server, pull out hardware details (Dell specific)

This is pretty much identical to the last script, but it filters the output to return only Dell servers. It’s just the extra chunk that starts “If($madeBy -eq “Dell Inc.”)”. This could be replaced with any manufacturer. I neede to send our live estate to our account manager.

Because WMI is a bit weird, this script has to pull data out of different classes so looks worse than it is. It has to use the NETBIOS name to get all this data. It’s based around an array that is created from the number of servers it find (“OperatingSystem=*server*”). The $mergeDetails looks complicated, but it’s the only way I could find of presenting the results neatly, so the details of each server is on a seperate line. It’s very easy to just output the array in a list format but is horrible to read.

The main “If” loop just determines whether the server is respoding- if not, it writes to the same file that it can’t retrieve any data.
————————————————–
Remove-Item .\Dell_Name_ST_Model.csv

[string]$serverList
[string]$serverNames
[string]$serverArray
[int]$xAxis
[int]$yAxis
[string]$mergeDetails

$serverList = Get-ADComputer -LDAPFilter “(&(ObjectCategory=Computer)(OperatingSystem=*server*))” | Select-Object Name | Sort-Object Name
$serverNames = $serverList.Name

$serverArray = New-Object ‘object[,]’ $serverList.Count,4
$xAxis = 0
$yAxis = 0

ForEach ($server in $serverNames)
{

$isAlive = Test-Connection $server -Count 1 -Quiet

If($isAlive -eq $true)
{
$madeBy = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server -ErrorAction ‘SilentlyContinue’ | Select-Object Manufacturer -ExpandProperty Manufacturer

If($madeBy -eq “Dell Inc.”)
{
$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server | Select-Object CSName -ExpandProperty CSName
$mergeDetails = $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Manufacturer -ExpandProperty Manufacturer
$mergeDetails = $mergeDetails + “,” + $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Model -ExpandProperty Model
$mergeDetails = $mergeDetails + “,” + $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_SystemEnclosure -ComputerName $server | Select-Object SerialNumber -ExpandProperty SerialNumber
$mergeDetails = $mergeDetails + “,” + $serverArray[$xAxis,$yAxis]
$mergeDetails | Out-File .\Dell_Name_ST_Model.csv -Append
}
}
ElseIf($isAlive -eq $false)
{
$mergeDetails = $server + ” ,is not responding so I can’t retrieve any data” | Out-File .\Dell_Name_ST_Model.csv -Append
}

$yAxis=0
$xAxis++
}

Trawl AD for devices running Windows Server, pull out hardware details

Because WMI is a bit weird, this script has to pull data out of different classes so looks worse than it is. It has to use the NETBIOS name to get all this data. It’s based around an array that is created from the number of servers it find (“OperatingSystem=*server*”). The $mergeDetails looks complicated, but it’s the only way I could find of presenting the results neatly, so the details of each server is on a seperate line. It’s very easy to just output the array in a list format but is horrible to read.

The main “If” loop just determines whether the server is respoding- if not, it writes to the same file that it can’t retrieve any data.
———————————————–
Remove-Item .\allServers_Name_ST_Model.csv

[string]$serverList
[string]$serverNames
[string]$serverArray
[int]$xAxis
[int]$yAxis
[string]$mergeDetails

$serverList = Get-ADComputer -LDAPFilter “(&(ObjectCategory=Computer)(OperatingSystem=*server*))” | Select-Object Name | Sort-Object Name
$serverNames = $serverList.Name

$serverArray = New-Object ‘object[,]’ $serverList.Count,4
$xAxis = 0
$yAxis = 0

ForEach ($server in $serverNames)
{

$isAlive = Test-Connection $server -Count 1 -Quiet

If($isAlive -eq $true)
{
$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server | Select-Object CSName -ExpandProperty CSName
$mergeDetails = $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Manufacturer -ExpandProperty Manufacturer
$mergeDetails = $mergeDetails + “,” + $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object Model -ExpandProperty Model
$mergeDetails = $mergeDetails + “,” + $serverArray[$xAxis,$yAxis]
$yAxis++

$serverArray[$xAxis,$yAxis] = Get-WMIObject -Class Win32_SystemEnclosure -ComputerName $server | Select-Object SerialNumber -ExpandProperty SerialNumber
$mergeDetails = $mergeDetails + “,” + $serverArray[$xAxis,$yAxis]
$mergeDetails | Out-File .\allServers_Name_ST_Model.csv -Append
}
ElseIf($isAlive -eq $false)
{
$mergeDetails = $server + ” ,is not responding so I can’t retrieve any data” | Out-File .\allServers_Name_ST_Model.csv -Append
}

$yAxis=0
$xAxis++
}