March 2004

Check Service Status on Local or Remote Servers

Building the script one piece at a time
RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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 LAN—just 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 it—numbered in order starting with 0—in 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 them—I wrote GetServiceStatus.vbs to assign the argument's value to a variable only to make the argument easy to work with.

   Previous  [1]  2  3  Next 


Windows IT Pro Community
Blogs





Top Viewed ArticlesView all articles
10 Reasons to Deploy Windows Vista

The decision to upgrade your XP systems to Vista is simple when you consider features such as easier backup, a great desktop search, and vastly improved security options. ...

10 Reasons Not to Deploy Windows Vista

The decision to upgrade to Vista has to make business sense, but many companies find the costs in training and application compatibility problems outweigh any benefits Vista brings. ...

CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


SQL Server Magazine Office & SharePoint Pro Windows Dev Pro ITTV
IT Library Technology Resource Directory Connected Home Windows SuperSite
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing