Subscribe to Windows IT Pro
April 11, 2005 12:00 AM

Are the OS Service Packs on Your Remote Computers Up-to-Date?

WMI-based scripts make it simple to find out
Windows IT Pro
InstantDoc ID #45818
Rating: (0)
Downloads
45818.zip

Windows systems administrators have to make sure that the software on their systems is current. The Windows service pack is one of the most important factors in determining whether the OSs on managed computers are up-to-date. IT shops with large budgets can afford management software such as Microsoft Systems Management Server (SMS) that distributes and reports on Windows service packs. However, small shops might not have the budget or expertise to manage an enterprise-level patch-management solution like SMS.

I've written two VBScript scripts that let you query remote computers for their installed OSs and service packs. The first script, GetSP.vbs, connects to a single computer and reports on that computer's installed OS and service pack. The second script, SPCheck.vbs, reads a list of computers from a text file and creates a report suitable for import into a spreadsheet or database.

The remote computers must be running Windows 2000 or later. The computer executing these scripts must be running Win2K with VBScript 5.6, or Windows XP or later. (VBScript 5.6 is part of the OS in XP and later.) The Win2K VBScript 5.6 installer (scripten.exe) is available for download on Microsoft's Web site. Note that if you install Microsoft Internet Explorer (IE) 6.0 or later on Win2K, VBScript 5.6 is installed.

Using GetSP.vbs
I wrote GetSP.vbs to illustrate how to use Windows Management Instrumentation (WMI) to obtain information from a remote computer. Although you can use other tools (e.g., Sysinternals' PsInfo) to retrieve information from remote computers, you typically need to parse the output to retrieve just the service pack version. A short WMI-based script like GetSP.vbs can provide relevant details without relying on other tools. In addition, you can easily customize the script as needed.

The command-line syntax to run GetSP.vbs is

GetSP.vbs [computername]

where computername is the name of the computer for which you want to obtain the OS and service pack version. If you don't provide a computer name, GetSP.vbs will obtain the information from the local computer.

Inside GetSP.vbs
As Listing 1 shows, GetSP.vbs starts by using the Option Explicit statement to force variable declaration. Next, the script defines the SCRIPT_NAME constant and calls the Main subroutine, which does the bulk of the script's work.

Callout A in Listing 1 shows the Main subroutine. This subroutine declares several variables, then uses the WScript.Arguments collection to parse the command line. In keeping with standard conventions, when the /? option is present, GetSP.vbs displays a short usage message and exits. When the command line contains at least one unnamed argument (i.e., an argument that doesn't start with the / character), the script uses that argument as the computer name. Otherwise, the script assumes you want to use the local computer.

Next, the script disables the default error handler with the On Error Resume Next statement because the script itself will handle any errors. It then uses VBScript's GetObject function to connect to the WMI provider on the target computer. InstancesOf is a WMI method that returns a collection of objects of the specified class. In this case, the script obtains a collection of WMI objects from the Win32_OperatingSystem class in the root\CIMV2 namespace.

If the GetObject function call fails, the script engine raises an error. GetSP.vbs uses the Err object to check for this error condition. The Err object's default property is Number, so you don't have to write it explicitly. When the Err object's Number property is nonzero, the script exits gracefully with an error message. When no error occurs, the script uses the On Error GoTo 0 statement to re-enable the default error handler.

At this point, the objSystems variable contains a collection of Win32_OperatingSystem class objects. GetSP.vbs uses a For Each...Next statement to iterate through the collection, even though, in this particular case, the collection contains only one member—the object representing the target computer. The script then uses the WScript object's Echo method to output information about that computer. It reads the CSName property to retrieve the computer's name, the Description property to obtain the computer's description (if defined), the Caption property to get the OS name, and the ServicePackMajorVersion property to obtain the service pack version. (For more information about these and other Win32_OperatingSystem class properties, go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_operatingsystem.asp.)

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.