Monitoring Processes for Availability

Microsoft® Windows® 2000 Scripting Guide

Availability is the simplest form of process monitoring: with this approach, you simply ensure that the process is running. When you monitor for process availability, you typically retrieve a list of processes running on a computer and then verify that a particular process is still active. If the process is active, it is considered available. If the process is not active, it is not available.

The Win32_Process class enables you to create scripts that identify the processes currently running on any computer in your organization.

Scripting Steps

Listing 14.1 contains a script that monitors process availability by checking the list of processes running on a computer and issuing a notification if the Database.exe process is not found. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Process class.

    To restrict data retrieval to a single process, a WHERE clause is used to limit data retrieval to the process with the name Database.exe.

  4. Use the Count method to determine the number of processes retrieved.

    The Count method provides a quick way to determine whether Database.exe is running. If Count is equal to 1, that means one instance of Database.exe is running on the computer. If Count is equal to 0, no instances of Database.exe are running on the computer.

  5. If no instances of Database.exe are detected, a message to that effect is echoed to the screen. Otherwise, the script echoes the fact that Database.exe is running.

Listing 14.1 Monitoring Process Availability

  
1
2
3
4
5
6
7
8
9
10
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process WHERE Name = 'Database.exe'")
If colProcesses.Count = 0 Then
 Wscript.Echo "Database.exe is not running."
Else
 Wscript.Echo "Database.exe is running."
End If