Hey, Scripting Guy! We have a bunch of Windows Server 2003 computers in our organization. Some of these computers are running Windows Server 2003 R2 and some are not. How can I tell which of our Windows Server 2003 computers are running R2?
— TX
Hey, TX. You know, that’s a question that people have been trying to answer for hundreds and hundreds of years: how can I determine whether a computer is running Windows Server 2003 R2 or not? And we’re not exaggerating here, either: after all, who could ever forget Julius Caesar’s last words: “R2, Brute?”
Now, if a Julius Caesar had no idea how to determine whether or not a computer is running Windows Server 2003 R2, what makes anyone think the Scripting Guys can succeed where even the mighty Caesar failed? Well, it’s definitely true that Caesar had wealth, power, and a genius for military tactics. What he didn’t have, however, was WMI:
strComputer = “.”Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)
For Each objItem in colItems If InStr(objItem.OtherTypeDescription, “R2”) Then WScript.Echo “This computer is running Windows Server 2003 R2.” Else WScript.Echo “This computer is not running Windows Server 2003 R2.” End If Next
Yes, we know: it seems like this script should be a little fancier than that, doesn’t it? But this will do the trick, and in just a second we’ll explain how, and why.
The script starts off like most WMI scripts: it simply makes a connection to the WMI service on the local computer. (And, like most WMI scripts it can easily be modified to run against a remote computer: just assign the name of that remote computer to the variable strComputer.) We then use this line of code to retrieve information about the operating system currently in use on the machine:
Set colItems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)
Now comes the part that will have people singing the praises of the Scripting Guys long after they have forgotten Julius Caesar. As it turns out, the Win32_OperatingSystem class includes a property named OtherTypeDescription. Throughout recorded history this property has always returned a Null value. (Try it on a computer running Windows 2000 Server or Windows XP or regular old Windows Server 2003. You’ll always get back the same value: Null.) On a computer running Windows Server 2003 R2, however, this property does return a value. Not only that, but that value includes the string R2. And that’s how you determine whether or not a computer is running R2: you simply check to see if the string R2 can be found anywhere within the OtherTypeDescription property.
Needless to say, then, that’s what we do with the rest of the script. We set up a For Each loop to walk through the collection of operating systems returned by our query. (There will be only one such operating system, but we still need to use a For Each loop, something which will not be the case in Windows Vista.) We then use the InStr function to check for the presence of the string R2 in the OtherTypeDescription property:
If InStr(objItem.OtherTypeDescription, “R2”) Then
If InStr returns True we report back the fact that the computer is running R2; otherwise we report back the fact that the computer is not running R2. That’s all we have to do.
So is this the most important script ever written? Well, we wouldn’t go quite that far. On the other hand, Julius Caesar didn’t know about the OtherTypeDescription property and look what happened to him. So on second thought ….
0 comments