How Can I Get a List of All My Windows Server 2003 Computers?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I gather the names of all the computers in my domain that are running Windows Server 2003?

— AS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AS. When we choose questions to answer for this column, we try to take questions from different areas of scripting. That does two things for us: it keeps us from being the answers-only-questions-about-say-scripting-Active-Directory column, and it ensures that we don’t sound like we’re providing the same answer to the same question day after day. Of course, on the other hand, we get lazy every now and then, and when we do, we usually fall back on one of our old standbys: search Active Directory.

As you might have guessed, we’re feeling lazy today. How can you gather the names of all the computers in your domain that are running Windows Server 2003? Search Active Directory.

Here’s a script that echoes back the names of all the computers in the fabrikam.com domain that are running Windows Server 2003:

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject(“ADODB.Connection”) Set objCommand = CreateObject(“ADODB.Command”) objConnection.Provider = “ADsDSOObject” objConnection.Open “Active Directory Provider”

Set objCommand.ActiveConnection = objConnection objCommand.CommandText = _ “SELECT Name FROM ‘LDAP://DC=fabrikam,DC=com’ WHERE objectClass=’computer’ ” & _ “and operatingSystemVersion = ‘5.2 (3790)'” objCommand.Properties(“Page Size”) = 1000 objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREE Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst

Do Until objRecordSet.EOF Wscript.Echo “Computer Name: ” & objRecordSet.Fields(“Name”).Value objRecordSet.MoveNext Loop

As with all Active Directory search scripts, the key to this script lies in the SQL query. In this query, we’re looking for all the computers (objectClass=’computer’) where the operating system version happens to be Windows Server 2003 (operatingSystemVersion = ‘5.2 (3790)’). We’re simply returning the names of all computers where the operatingSystemVersion attribute has a value of 5.2 (3790); that happens to be the way the version number for Windows Server 2003 is stored in Active Directory.

So how did we know that the version number for Windows Server 2003 is stored in Active Directory as 5.2 (3790)? Hey, come on, we’re the Scripting Guys; we know everything!

Well, OK: we opened up Active Directory Users and Computers and found a computer that we knew was running Windows Server 2003. We right-clicked the computer name, clicked Properties, and then looked on the Operating System tab to find the operating system version.

We can also use a script to grab all the operating systems and their version numbers from Active Directory. Unfortunately, Active Directory doesn’t support SQL’s SELECT DISTINCT query type, so we can’t pull out just the 2 or 3 or 4 operating systems in use; instead, we have to grab all the computers and their operating systems, and then look through the list to pull out the unique versions. But this might be a good exercise for you: see if you can find a way to put some sort of filtering into this script so it shows only the unique (distinct) operating system versions. (One hint: You might consider creating a Dictionary object and storing the versions there. That way, you could easily check to see if any one version is already in the Dictionary. If it is, then don’t add a duplicate entry.)

Oh, right; here’s the script:

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject(“ADODB.Connection”) Set objCommand = CreateObject(“ADODB.Command”) objConnection.Provider = “ADsDSOObject” objConnection.Open “Active Directory Provider”

Set objCommand.ActiveConnection = objConnection objCommand.CommandText = _ “Select operatingSystem, operatingSystemVersion from ” & _ “‘LDAP://DC=fabrikam,DC=com’ where objectClass=’computer’ ORDER BY operatingSystem” objCommand.Properties(“Page Size”) = 1000 objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREE Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst

Do Until objRecordSet.EOF Wscript.Echo objRecordSet.Fields(“operatingSystem”).Value, _ objRecordSet.Fields(“operatingSystemVersion”).Value objRecordSet.MoveNext Loop

0 comments

Discussion is closed.

Feedback usabilla icon