PowerShell: Collect AD Joined Server Information

Recently I needed to perform an inventory of servers joined to an AD Domain. I needed to know some basic hardware information, the server location and version of Windows installed. Being a scripter, I knew I could easily accomplish this task using PowerShell and WMI.


The script will query AD for all machines running server builds of Windows, then collect the following information about each one and export it to CSV:

The script will also provide a handy status bar of how many servers it has to query, how many servers it has already queried and the server it is currently queried. Finally, make sure you run the script with an account with rights to access the servers, otherwise you won't get much data back! And now, the code:

$Results = @()
$i = 1

Write-Host "Querying Active Directory for servers..."
$Servers = Get-ADComputer -Filter 'OperatingSystem -like "*Server*" -and Enabled -eq $true' -Properties Name,OperatingSystem

ForEach ($Server in $Servers)
{
Write-Progress -Activity "Compiling Server Info"  -Status "Working on $($Server.Name) ($i of $($Servers.Count))..." -PercentComplete (($i/$Servers.Count)*100)
$IP = Test-Connection $Server.Name -Count 1 -Quiet
If ($IP)
{
$IPAddr = $IP.IPV4Address
$ADSite = (nltest /Server:"$($Server.Name)" /DSGetSite)[0]
$CPUData = Get-WmiObject -Class Win32_Processor -Namespace "root\CIMV2" -ComputerName $Server.Name
$OSInfo = Get-WmiObject -Class Win32_OperatingSystem -Namespace "root\CIMV2" -ComputerName $Server.Name
$SysInfo = Get-WmiObject -Class Win32_ComputerSystem -Namespace "root\CIMV2" -ComputerName $Server.Name

# Get the CPU count
If ($CPUData)
{
If (($Server.OperatingSystem -match "2000") -or ($Server.OperatingSystem -match "2003"))
{
$CPU = ($CPUData | select -Unique SocketDesignation).Count
If (-not $CPU)
{$CPU = 1}
}
Else
{
$CPU = $CPUData.Count
If (-not $CPU)
{$CPU = 1}
}
}
Else
{$CPU = "Error"}

If ($Server.OperatingSystem)
{$OS = $Server.OperatingSystem}
Else
{$OS = "N/A"}

If ($OSInfo)
{
# Get the OS edition
If (($OSInfo.Caption) -match "Enterprise")
{$OSEd = "Enterprise"}
ElseIf (($OSInfo.Caption) -match "Standard")
{$OSEd = "Standard"}
ElseIf (($OSInfo.Caption) -match "Datacenter")
{$OSEd = "Datacenter"}
ElseIf (($OSInfo.Caption) -match "Preview")
{$OSEd = "Preview"}
Else
{$OSEd = "Unknown"}
}
Else
{
$OSEd = "Error"
}

If ($SysInfo)
{
# Physical or Virtual
If (($SysInfo.Model) -match "Virtual")
{$Platform = "Virtual"}
Else
{$Platform = "Physical"}
$Manufacturer = $SysInfo.Manufacturer
$Model = $SysInfo.Model
}
Else
{$Platform = "Error"}
}
Else
{
$IPAddr = "N/A"
$ADSite = "N/A"
$Platform = "N/A"
$Manufacturer = "N/A"
$Model = "N/A"
$CPU = "N/A"
$OS = "N/A"
$OSEd = "N/A"
}

$Info = New-Object System.Object
$Info | Add-Member -Type NoteProperty Name $Server.Name
$Info | Add-Member -Type NoteProperty IP $IPAddr
$Info | Add-Member -Type NoteProperty ADSite $ADSite
$Info | Add-Member -Type NoteProperty Platform $Platform
$Info | Add-Member -Type NoteProperty Manufacturer $Manufacturer
$Info | Add-Member -Type NoteProperty Model $Model
$Info | Add-Member -Type NoteProperty CPUCount $CPU
$Info | Add-Member -Type NoteProperty OS $OS
$Info | Add-Member -Type NoteProperty OSEdition $OSEd
$Results += $Info

$i++
}

$Results | Export-Csv "C:\Temp\ServerInfo.csv" -NoTypeInformation

Labels: , , , , ,