PowerShell Server Inventory Script


I was asked for my Server Inventory script on the newsgroup Microsoft.Public.Windows.PowerShell.

This is my script, in a slightly compressed and sanitized version – Compressed as I stripped out Novell, Unix, and ESX Host logic, sanitized in that I don’t really have a domain contoso.com 😉

After the script, if you stick around, I will explain the “Location” information, as well as anything else I can think of.

Normally I’d put functional comments in my scripts, I have no idea how I managed to avoid that in this script.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Function WMILookup 
{
$AllServers = @()
$ServerObj  = @()
foreach ($StrComputer in $ComputerDescription.Keys)
{
$GenItems1  = $null
$SysItems1 = $null
$ProcItems1 = $null
$DiskItems = $null
$NetItems = $null
$Current += 1
Write-Output "Working on $StrComputer"
$GenItems1 = gwmi Win32_ComputerSystem -Comp $StrComputer
$ServerObj = New-Object psObject
$ServerObj |Add-Member -MemberType NoteProperty -Name "GUID" -Value $ComputerGUID[$strComputer] -Force
if ($GenItems1 -eq $null)
{
$ServerObj | Add-Member -membertype noteproperty -name "Hostname" -Value  $StrComputer -Force
if ($ComputerLastLogon[$StrComputer] -eq $null)
{
$ServerObj | Add-Member -membertype noteproperty -name "Manufacturer" -Value "No WMI Data" -Force
}
if ($ComputerLastLogon[$StrComputer] -ne $null)
{
$lastLogon = ""
$lastLogon = $ComputerLastLogon[$StrComputer]
$ServerObj | Add-Member -membertype noteproperty -name "Manufacturer" -Value "No WMI Data – Last logged in: $lastlogon" -Force
}
$ServerObj |Add-Member -MemberType NoteProperty -Name "Home OU" -Value $ComputerOU[$strComputer] -Force
$ServerObj |Add-Member -MemberType NoteProperty -Name "Function" -Value $ComputerDescription[$strComputer] -Force
$ServerObj |Add-Member -MemberType NoteProperty -Name "GUID" -Value $ComputerGUID[$strComputer] -Force
$AllServers += $ServerObj
}
if ($GenItems1 -ne $null)
{
$GenItems2 = gwmi Win32_OperatingSystem -Comp $StrComputer
$SysItems1 = gwmi Win32_BIOS -Comp $StrComputer
$ProcItems1 = @(gwmi Win32_Processor -Comp $StrComputer)
$DiskItems = @(gwmi Win32_LogicalDisk -Comp $StrComputer | Where-Object {$_.DriveType -eq 3})
$NetItems = @(gwmi Win32_NetworkAdapterConfiguration -Comp $StrComputer | where{$_.IPEnabled -eq "True"} | select @{name=‘IP Address’;expression={$_.IPAddress}},MACAddress)
foreach ($objItem in $GenItems1)
{
$ServerObj | Add-Member -membertype noteproperty -name "Hostname" -Value  $objItem.Name.Trim() -Force
$ServerObj | Add-Member -membertype noteproperty -name "Manufacturer" -Value $objItem.Manufacturer.Trim() -Force
$ServerObj | Add-Member -membertype noteproperty -name "Model" -Value $objItem.Model.Trim() -Force
$Memory = $objItem.TotalPhysicalMemory /1024/1024/1024
$Memory  = "{0:n0}" -f $Memory
$Memory = $Memory +" (GB)"
$ServerObj | Add-Member -MemberType NoteProperty -Name "RAM" -Value $Memory -Force
$ServerObj | Add-Member -membertype noteproperty -name "CPU’s" -Value $objItem.NumberOfProcessors -Force
}
foreach ($objItem in $GenItems2)
{
$ServerObj | Add-Member -membertype noteproperty -name "OS" -Value $objItem.Caption -Force
}
foreach ($objItem in $SysItems1)
{
$ServerObj | Add-Member -membertype noteproperty -name "Serial Number" -Value $objItem.SerialNumber -Force
}
$ServerObj | Add-Member -MemberType NoteProperty -Name "City" -Value $ComputerLocation[$strComputer].Split(";")[3] -Force
$ServerObj | Add-Member -MemberType NoteProperty -Name "Building" -Value $ComputerLocation[$strComputer].Split(";")[2] -Force
$ServerObj | Add-Member -MemberType NoteProperty -Name "Room" -Value $ComputerLocation[$strComputer].Split(";")[1] -Force
$ServerObj | Add-Member -MemberType NoteProperty -Name "Rack" -Value $ComputerLocation[$strComputer].Split(";")[0] -Force
$ServerObj | Add-Member -membertype NoteProperty -Name "CPU Speed" -Value $ProcItems1[0].maxClockSpeed -Force

$intRowDisk = 0
[STRING]$diskSizeTotal = @()
$ServerObj | Add-Member -membertype noteproperty -name "Disk Drives" -Value $DiskItems.Length -Force
foreach ($objItem in $DiskItems)
{
$diskSize = $objItem.Size/1024/1024/1024
$diskSize = "{0:n2}" -f $diskSize
$intRowDisk = $intRowDisk + 1
$diskSizeTotal += $objItem.DeviceID + " " + $diskSize + " (GB); "
}
$ServerObj | Add-Member -membertype noteproperty -name "Disk Space" -Value $diskSizeTotal.substring(0,$diskSizeTotal.LastIndexOf(";"))  -Force
$intRowNet = 0
$ServerObj | Add-Member -membertype noteproperty -name "NIC’s" -Value $NetItems.Length -Force
[STRING]$MACAddresses = @()
[STRING]$IpAddresses = @()
foreach ($objItem in $NetItems)
{
if ($objItem.{IP Address}.Count -gt 1)
{
$TempIpAdderesses = [STRING]$objItem.{IP Address}
$TempIpAdderesses  = $TempIpAdderesses.Trim().Replace(" ", " ; ")
$IpAddresses += $TempIpAdderesses # +"; "
}
else
{
$IpAddresses += $objItem.{IP Address} +"; "
}
if ($objItem.{MacAddress}.Count -gt 1)
{
$TempMACAddresses = [STRING]$objItem.{MACAddress}
$TempMACAddresses = $TempMACAddresses.Replace(" ", " ; ")
$MACAddresses += $TempMACAddresses +"; "
}
else
{
$MACAddresses += $objItem.{MACAddress} +"; "
}
$intRowNet = $intRowNet + 1
}
$ServerObj | Add-Member -membertype noteproperty -name "IP Address" -Value $IpAddresses.substring(0,$ipaddresses.LastIndexOf(";")) -Force
$ServerObj | Add-Member -membertype noteproperty -name "MAC Address" -Value $MACAddresses.substring(0,$MACAddresses.LastIndexOf(";")) -Force
$ServerObj | Add-Member -MemberType NoteProperty -Name "Function" -Value $ComputerDescription[$strComputer] -Force
$AllServers += $ServerObj
}
}
$file =  ( get-date ).ToString(‘yyyy-MM-dd’) + "-ServerInventory.csv"
$AllServers |Sort-Object "HostName" | Export-Csv "c:\scripts\blog\$file" -NoTypeInformation
}
Function ListADServers 
{
$ComputerDescription = @{}
$ComputerOU = @{}
$ComputerGUID = @{}
$ComputerLastLogon = @{}
$ComputerLocation = @{}
$Results = @()
$DomainName = ‘LDAP://OU=Computers,DC=Contoso,DC=com’
$Root = New-Object DirectoryServices.DirectoryEntry $DomainName
$objSearcher = New-Object DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $Root
$objSearcher.SearchScope = "SubTree"
$objSearcher.Filter =  "(&(objectCategory=computer)(OperatingSystem=Windows*Server*))"
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objComputer = $objResult.Properties
$computer = $objResult.GetDirectoryEntry()
$ComputerDescription.Add([string]$computer.name, [string]$computer.description)
$ComputerLocation.Add([string]$computer.name, [string]$computer.location)
$ComputerGUID.Add([string]$computer.name, $computer.psbase.guid.ToString())
if ($objComputer.lastlogon -ne $null)
{
[string]$lastLogonInterval = $objComputer.lastlogon
}
elseif ($objComputer.lastlogon -eq $null)
{
[string]$lastLogonInterval = $objComputer.lastlogontimestamp
}
$lastLogon = [System.DateTime]::FromFileTime($lastLogonInterval)
$ComputerLastLogon.Add([string]$computer.name, $lastLogon.Date.ToShortDateString())
}
}
$erroractionpreference = "SilentlyContinue"
. ListADServers
WMILookup

Now, as promised, the location explanation. Here at “Contoso” 😉 We have servers in multiple data centers in multiple cities. We have found that the easiest way to keep track of these servers is to populate the “Location” attribute in Active Directory as follows:

Rack;Room;Building;City

I suppose we could get clever with the network information and get close to the same information, but this works for us.

Note that there is far more information available from each WMI call than I preserve here.

Try this:

Get-WmiObject Win32_ComputerSystem| Format-List *

You can gather way more than I gather, so poke around a bit, and see what data you require.

Advertisements
  1. #1 by Alan Renouf on December 14, 2009 - 14:26

    Nice script, I wrote a WMI based audit script for multiple computers and added a nice html output here.. http://www.virtu-al.net/2009/10/26/workstation-server-audit-script-v3/

    • #2 by karlmitschke on December 14, 2009 - 21:37

      Thanks for the comment. Your HTML output looks nice.

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

%d bloggers like this: