Frequently, when something just isn't right on a computer, you can trace the problem to a service that's stopped running. Checking the status of services to make sure they're running is a useful troubleshooting step. On a local machine, you simply open the Microsoft Management Console (MMC) Computer Management snap-in and browse to Services and Applications, Services. But how can you quickly and easily check services on remote computers? If you're using Windows 2000 Server Terminal Services in Remote Administration mode, you can use Microsoft's Remote Desktop Connection software, but doing so requires you to perform additional steps. Furthermore, this option is available only on computers that accept incoming RDP requests. Particularly if you want to inventory many computers, a better solution is to write a script that lets you check the status of a service on any computer that supports Windows Management Instrumentation (WMI).
In "Windows Management Instrumentation for Beginners," May 2001, http:www.winnetmag.com, InstantDoc ID 20376, I introduced WMI by using it to streamline your computer management tasks. WMI is an interface to many physical and logical parts of a computer that the WMI service exposes. Now, I want to show you how to build a script called GetServiceStatus.vbs that attaches to WMI on a server and uses it to enumerate the services on that machine. You can configure GetServiceStatus.vbs to check the status of the local server, a remote server, a group of remote servers, or all servers in the domain. As I explain the workings of GetServiceStatus.vbs, I also talk about ways to organize data, the use of arguments in scripts, and some constants you can use to format output with tabs and carriage returns.
Gather Service Status
GetServiceStatus.vbs's core objectives are to attach to a specific server, enumerate the available services on that server, and display the status of each service. Everything else the script does contributes to specifying a set of servers to check.
To accomplish the core tasks, I assign the name of a server to a variable (sComputerName), connect to the WMI namespace on that server, and log on with administrative privileges so that I can access the information I want. Then, I retrieve a complete list of all services on the server and display each service's name and current state (e.g., started, stopped, paused) separated by a tab. Listing 1 shows how the script checks for services. I've hard-coded the server name (i.e., Alien) into this code snippet, but the actual script is more flexible, as you'll see.
As long as you have administrative rights on the domain, the code in Listing 1 lets you connect to the WMI service on another computer simply by changing the value of sComputerName to the name of the computer on which you want to run the code. The script will take slightly longer to run on a remote computer than on a local one but not significantly longer over a LANjust long enough to connect to the service on the remote computer. The computer on which you run this code will produce output that lists all the services that are running. Figure 1 shows a bit of sample output.
However, you don't want to edit the script every time you want to check service status on a different computer. If you configure the script to use computer names supplied as arguments, stored in a text file, or collected from Active Directory (AD), your script will be able to report service status for everything from one particular server to every computer running WMI that the script can communicate with.
Computer Names as Arguments
If you want to specify just a few server names at a time, the simplest approach is to have the script prompt you to supply those names as arguments. Keep in mind that, to VBScript, any collection of characters separated by any number of spaces is a separate argument, and an argument that contains a space must be within quotation marks.
VBScript stores every argument you supply to itnumbered in order starting with 0in a collection called Wscript.Arguments. You specify the argument that you want to use from the collection by using the collection name followed by the argument's ordinal number in parentheses. Thus, Wscript.Arguments(0) specifies the first argument in the collection, and Wscript.Arguments(3) specifies the fourth argument. Even if the collection contains only one argument, you must still specify the index number of that argument when referring to it in the script. Your script doesn't need any special code to accept arguments or store themI wrote GetServiceStatus.vbs to assign the argument's value to a variable only to make the argument easy to work with.
Andrew Whitton May 14, 2004