How Can I Determine if a Computer is Running Windows Server 2003 and if Service Pack 1 is Installed?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine whether or not a computer is running Windows Server 2003 and, if so, whether Service Pack 1 is installed?

— TW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TW. You know, we Scripting Guys always get a kick out of questions like this. Not because there’s anything wrong with the question, mind you. It’s just that many of the people we work with would have no idea what you’re talking about here. After all, why would you need a script like this one: of course you have Windows Server 2003 installed, and of course you’ve got Service Pack 1 on there as well. I mean, even your laptops are running Windows Server 2003, Service Pack 1. Aren’t they?

We Scripting Guys like to think we’re a little more in touch with reality than some of our fellow Microsoft employees. (Now that’s something you don’t hear every day!) We understand that people – for many valid reasons – don’t always upgrade the very second that Microsoft releases a new product or a new version of a product. That’s why, whenever possible, we try to provide a Windows 2000 workaround in those columns where we show people a Windows XP or Windows Server 2003 solution. And that’s why we’re more than happy to show you a script that can tell you whether a computer is running Windows Server 2003 and, if so, whether it has Service Pack 1 installed:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_OperatingSystem”)

For Each objItem in colItems If InStr(objItem.Caption, “Server 2003”) Then strText = objItem.Caption If objItem.ServicePackMajorVersion = “1” Then strText = strText & “, Service Pack 1” End If Wscript.Echo strText Else Wscript.Echo “This computer is not running Windows Server 2003.” End If Next

Pretty exciting, huh? The script starts out by connecting to the WMI service on the local computer; for those of you with multiple machines in multiple locations, this script can also be used to determine the operating system installed on a remote computer. We then use this line of code to retrieve a collection of all the operating systems installed on the computer:

Set colItems = objWMIService.ExecQuery _
    (“Select * from Win32_OperatingSystem”)

Note. OK, we admit it: this query doesn’t return a collection of all the operating systems installed on a computer; it only returns information about the operating system currently in use. That’s true even if you have a multi-boot computer: you’ll only be told about the current operating system, and won’t even be aware that other OSes are installed on the machine.

That’s just the way it goes.

Even though there will be only one operating system in our collection we still need to set up a For Each loop to walk through that collection. Logically enough, then, we set up a For Each loop and then run this line of code:

If InStr(objItem.Caption, “Server 2003”) Then

What we’re doing here is using the InStr function to determine whether or not the string Server 2003 can be found anywhere in the Caption property. We do this because Windows Server 2003 comes in various flavors: Standard, Advanced, Enterprise, Windows Server 2003 R2, Caramel Nut Fudge. The one thing all these versions have in common (well, except for Caramel Nut Fudge, which might not actually be a version of the operating system) is that the string Server 2003 appears somewhere in the caption. So that’s what we check for: if the Caption has Server 2003 in it then we assume it must be some flavor of Windows Server 2003. (And when we echo back the operating system information we’ll specify which flavor.)

Note. In case you’re wondering, no, we can’t just use the BuildNumber property, because the build numbers can also vary depending on the operating system version.

So what happens if we find the string Server 2003 in the Caption? That’s easy: we then store the entire Caption in a variable named strText. And because we did find the target string, that means that the computer in question must be running Windows Server 2003. Therefore, our next step is to see if the value of the ServicePackMajorVersion property is equal to 1; if it is, that means that Service Pack 1 is installed. That’s what we do here:

If objItem.ServicePackMajorVersion = “1” Then

If this is true (that is, if Service Pack 1 is installed), then we tack a note to that effect to end of the variable strText. That means that a computer that has Service Pack 1 installed will echo back information similar to this:

Microsoft(R) Windows(R) Server 2003, Standard Edition, Service Pack 1

If the computer is running Windows Server 2003 but doesn’t have Service Pack 1 installed then it will echo back something like this:

Microsoft(R) Windows(R) Server 2003, Standard Edition

See how that works? That’s how we distinguish between a computer with Service Pack 1 and a computer without Service Pack 1.

But what if the computer isn’t running Windows Server 2003 at all? Well, in that case our script will skip down to the Else statement and echo this message:

This computer is not running Windows Server 2003.

Sure, we know: nobody would ever have a computer that wasn’t running Windows Server 2003. But, just in case, we added this little feature to the script anyway. After all, if someone was abducted by aliens 6 years ago and were just recently returned, they might only now be getting around to installing Windows Server 2003 on all their machines. You never know.

0 comments

Discussion is closed.

Feedback usabilla icon