Extract Operating Systems from Active Directory
Prerequisites: ActiveRoles Management Shell for Active Directory must be installed on the host running this script.
On a forum I saw someone in need of a script that could display the operating systems of all the computers in Active Directory. Also it could help when you have to tell Microsoft how many licenses you need to pay for. This is an easy one so why not?
First I want to load the snappin so I don’t have to open the ActiveRoles Management Shell everytime I want to run the script.
Add-PSSnapin -name Quest.ActiveRoles.ADManagement
Next I’m going to fetch all computer accounts from Active Directory. For that I’m using the ‘Get-QADComputer’ cmdlet. You can add the attribute ‘-SearchRoot’ if you don’t want to search the entire Active Directory.
'Fetching computer accounts from Active Directory. This may take a while...' $computers = Get-QADComputer | Sort-Object Name '' + $computers.length + ' computer accounts fetched from Active Directory.' '-----------------------------'
The abvious thing to do now is to run through the computers array and check their operating system, but I need a structure to save the statistics. For that I’m going to use a hashtable. In that way I will be able to use the name of the operating system as the key. First I declare the hashtable.
$stats = @{}
And then I collect the stats.
foreach( $computer in $computers )
{
$OSName = $computer.OSName
if( $OSName -eq $null )
{
$OSName = "Unknown OS"
}
$stats[$OSName]++
#$computer.Name + "`t" + $computer.OSName + "`t" + $computer.OSVersion
}
As I run through the array I’m checking to see if the operating system value is $null. This is the case for e.g. the virtual names of a cluster service. I’m just going to list those as an unknown operating system. The physical nodes themselves will have the operating system property set. You can uncomment the last line if you want to display information about every single computer account.
That last thing to do is to display the collected statistics. You can just echo the $stats array, but if you want to sort the output you need to use ‘GetEnumerator()’.
You should get an output similiar to this:
Name Value ---- ----- Windows Vista™ Ultimate 1 Windows 7 Ultimate 1 Windows 2000 Server 2 Windows Server® 2008 Enterp... 3 Windows 7 Professional 3 Windows Server® 2008 Standard 6 Windows Vista™ Business 9 Windows Vista™ Enterprise 11 Windows 7 Enterprise 11 Unknown OS 24 Windows 2000 Professional 26 Windows Server 2003 52 Windows XP Professional 321
Please note that Windows Server 2003 isn’t devided into several categories like Windows Server 2008. So you don’t know how many is Standard or Enterprise Edition.
Here’s the entire script: Display Operating Systems
No comments yet.