Thursday, 27 August 2009

Quick and dirty server operating system audit.

I was sitting at my desk and I overhead a conversation about the Microsoft Licensing true up. There was a suggestion that someone would have to log into each server to find out what particular flavour of Windows is installed.

Now of course this information should be readily retrievable via WMI from the Win32_OperatingSystem class so I said I'd whip up a quick script that would produce a report.

As I remembered there is a property OperatingSystemSKU which holds this information, but it turns out to be Windows 2008 and beyond. After a bit of mucking around I discovered that the first part of the Name property value had the information I wanted. So it was simply a matter of snipping this out.

The script below uses the Quest Active Directory Cmdlets to query for all server computer objects, uses Win32_PingStatus to determine if the host associated with the object is actually responding, and does a WMI query for the details we want if it is. I decided to grab the O\S Architecture and Service Pack as well.

$now = $now = get-date -uformat "%Y%m%d"
$Domain = [DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$filename = "servers-$domain-$now.txt"
$computers = get-qadcomputer -sizelimit 0 -ldap '(operatingSystem=*server*)' %{$_.dnshostname}
Set-Content $filename "Report on server Operating System information in $domain created $now"
"HostName, SKU, Architecture, Service Pack" Add-Content $filename
ForEach($Computer in $Computers) {
$Result = Get-WmiObject -Class Win32_PingStatus -Filter "address='$computer'"
If ($Result.Statuscode -eq 0) {
If ($computer.length -ge 1) {
$WMI = GWMI -computer $computer -query "Select * from Win32_OperatingSystem"
$Name = ($WMI).Name
$Arch = ($WMI).OSArchitecture
$SPMaj = ($WMI).ServicePackMajorVersion
If ($Name -ne $null) {$NiceName = $Name.Substring(0,$Name.IndexOf(''))} Else {$NiceName = ""}
"$Computer, $NiceName, $Arch, $SPMaj" Add-Content $filename } }
Else { "$Computer did not respond." Add-Content $filename}}

0 comments:

Post a Comment