Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

process.vbs - vbscript script by ActiveXperts Software

process.vbs checks whether a process is running. Checks CPU and memory resources as well.

Use process.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select process.vbs. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


process.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // For more information about ActiveXperts Network Monitor and VBScript, visit
' // http://www.activexperts.com/support/network-monitor/online/vbscript/
' ///////////////////////////////////////////////////////////////////////////////

Option Explicit

' Declaration of global variables
Dim   SYSDATA, SYSEXPLANATION   ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column

' Constants - return values
Const retvalUnknown = 1         ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)

' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following lines:
' Dim bResult
' bResult =  CheckProcessMemory( "localhost", "", "explorer.exe", 2000 )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"


' The following contants are use in process/memory checks. 
' By default, the Private Working Set is used (which is also the default in the Windows Task Manager).
' Change the idxMemPrivateWorkingSet parameter in the code below to monitor a different type of process memory
Const idxMemPrivateWorkingSet = 0   ' Task Manager [Private Working Set] (default)
Const idxMemCommitSize = 1          ' Task Manager [Commit Size] (default)
Const idxMemWorkingSet = 2          ' Task Manager [Working Set]
Const idxMemPeakWorkingSet = 3      ' Task Manager [Peak Working Set]
Const idxMemPrivatePagedPool = 4    ' Task Manager [Memory - Paged Pool]
Const idxMemNonPagedPool = 5        ' Task Manager [Non-Paged Pool] 

Function CheckProcess( strHost, strAltCredentials, strProcess )
' Description: 
'     Checks if a process, specified by strProcess, is running on the machine specified by strHost. 
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcess As String - Name of the process
' Usage:
'     CheckProcess( "<Hostname | IP>", "<Empty String | Server>", "<Process>" )
' Sample:
'     CheckProcess( "localhost", "", "explorer.exe" )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckProcess    = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA         = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION  = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin     = ""
  strAltPassword  = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If
  
  CheckProcess    = checkProcessWMI( objWMIService, strHost, strProcess, SYSEXPLANATION )

End Function


' //////////////////////////////////////////////////////////////////////////////
      
Function CheckProcessMemory( strHost, strAltCredentials, strProcessName, nMaxMB )
' Description: 
'     This function checks if a process, specified by strProcess, is consuming less than nMaxMB of memory on computer specified by strHost. 
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcessName As String - Name of the process
'     4) nMaxMB As Number - Maximum allowed memory usage (MB)
' Usage:
'     CheckProcessMemory( "<Hostname | IP>", "<Empty String | Server>", "<Process>", Max_MB )
' Sample:
'     CheckProcessMemory( "localhost", "", "axnmsvc.exe", 30 )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckProcessMemory  = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA             = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION      = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin         = ""
  strAltPassword      = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If
  
  CheckProcessMemory = checkProcessMemoryWMI( objWMIService, strHost, strProcessName, idxMemPrivateWorkingSet, nMaxMB, SYSDATA, SYSEXPLANATION )

End Function 

' //////////////////////////////////////////////////////////////////////////////
      
Function CheckProcessHandles( strHost, strAltCredentials, strProcessName, nMaxHandles )
' Description: 
'     This function checks if a process, specified by strProcess, is holding less than nMaxHandles of handles on computer specified by strHost. 
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcessName As String - Name of the process
'     4) nMaxHandles As Number - Maximum allowed number of Handles )
' Usage:
'     CheckProcessHandles( "<Hostname | IP>", "<Empty String | Server>", "<Process>", Max_MB )
' Sample:
'     CheckProcessHandles( "localhost", "", "axnmsvc.exe", 300 )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckProcessHandles = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA             = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION      = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin         = ""
  strAltPassword      = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If
  
  CheckProcessHandles = checkProcessHandlesWMI( objWMIService, strHost, strProcessName, nMaxHandles, SYSDATA, SYSEXPLANATION )

End Function 

' //////////////////////////////////////////////////////////////////////////////

Function CheckAnyProcessMemory( strHost, strAltCredentials, nMaxMB )
' Description: 
'     This function checks if any process, is consuming more than nMaxMB of memory on computer specified by strHost. 
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) nMaxMB As Number - Maximum allowed memory usage (MB)
' Usage:
'     CheckAnyProcessMemory( "<Hostname | IP>", "<Empty String | Server>", Max_MB )
' Sample:
'     CheckAnyProcessMemory( "localhost", "", 100 )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckAnyProcessMemory   = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA                 = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION          = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin             = ""
  strAltPassword          = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If

  CheckAnyProcessMemory   = checkAnyProcessMemoryWMI( objWMIService, strHost, idxMemPrivateWorkingSet, nMaxMB, SYSDATA, SYSEXPLANATION )

End Function 

' //////////////////////////////////////////////////////////////////////////////

Function CheckProcessCpu( strHost, strAltCredentials, strProcess, numMaxCPU )

' Description: 
'     This function checks if a process doesn't consume more than the maximum specified CPU usage (%) 
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcess As String - Name of the process
'     4) numMaxCPU As Number - Maximum CPU usage (in %)
' Usage:
'     CheckProcessCpu( "<Hostname | IP>", "<Empty String | Server>", "<Process>", Max_CPU)
' Sample:
'     CheckProcessCpu( "localhost", "", "explorer.exe", 10 )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckProcessCpu   = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA           = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION    = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin       = ""
  strAltPassword    = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If
  
  CheckProcessCpu  = checkProcessCpuWMI( objWMIService, strHost, strProcess, numMaxCPU, SYSDATA, SYSEXPLANATION )

End Function

' //////////////////////////////////////////////////////////////////////////////

Function CheckNumProcesses( strHost, strAltCredentials, strProcess, numMinInstances, numMaxInstances )

' Description: 
'     This function counts the number processes - indicated by strProcess -on the computer specified by strHost.
'     If this count is less than numMinInstances or greater than numMaxInstances, an eror is generated. 
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcess As String - Name of the process
'     4) numMinInstances - minimum number of instances
'     5) numMaxInstances - maximum number of instances
' Usage:
'     CheckNumProcesses( "<Hostname | IP>", "<Empty String | Server>", "<Process>", Min_Instances, Max_Instances )
' Sample:
'     CheckNumProcesses( "localhost", "", "svchost.exe", 1, 5 )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckNumProcesses   = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA             = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION      = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin         = ""
  strAltPassword      = ""


  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If

  CheckNumProcesses  = checkNumProcessesWMI( objWMIService, strHost, strProcess, numMinInstances, numMaxInstances, SYSDATA, SYSEXPLANATION )

End Function



' //////////////////////////////////////////////////////////////////////////////
' // --- Private Functions section ---
' // Private functions names should start with a lower case character, so they 
' // will not be listed in the Network Monitor's function browser.
' //////////////////////////////////////////////////////////////////////////////

Function checkProcessWMI( objWMIService, strHost, strProcess, BYREF strSysExplanation )

  Dim objProcess, collProcesses

  checkProcessWMI        = retvalUnknown  ' Default return value

  On Error Resume Next

  set collProcesses      = objWMIService.ExecQuery( "select * from Win32_Process" ) 
  If( Err.Number <> 0 ) Then
    strSysData         = ""
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( collProcesses.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_Process class does not exist on computer [" & strHost & "]"
    Exit Function
  End If

  On Error Goto 0

  For Each objProcess in collProcesses
    If( Err.Number <> 0 ) Then
      checkProcessWMI    = retvalUnknown
      strSysExplanation  = "Unable to list processes on computer [" & strHost & "]"
      Exit Function 
    End If
    If UCase( objProcess.Name ) = UCase( strProcess ) Then
      checkProcessWMI    = True
      strSysExplanation  = "Process [" & strProcess & "] is running on computer [" & strHost & "]"
      Exit Function
    End If
  Next

  checkProcessWMI        = False
  strSysExplanation      = "Process [" & strProcess & "] is not running on computer [" & strHost & "]"

End Function


' //////////////////////////////////////////////////////////////////////////////

Function checkProcessHandlesWMI( objWMIService, strHost, strProcessName, nMaxHandles, BYREF strSysData, BYREF strSysExplanation )

    Dim objProcess, colProcesses, nUsageHandles, nHighUsageHandles, nDiffHandles, bProcessFound, strProcessNameShort

    checkProcessHandlesWMI         = retvalUnknown  ' Default return value
    bProcessFound                  = False
    nUsageHandles                  = 0
    nHighUsageHandles              = 0
    
On Error Resume Next

  Set colProcesses = objWMIService.ExecQuery( "Select * from Win32_PerfRawData_PerfProc_Process" )
  If( Err.Number <> 0 ) Then
    strSysData         = ""
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( colProcesses.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_PerfRawData_PerfProc_Process class does not exist on computer [" & strHost & "]"
    Exit Function
  End If

On Error Goto 0

  ' Win32_PerfRawData_PerfProc_Process lists processes without .exe, e.g. explorer instead of explorer.exe.
  strProcessNameShort = Replace( strProcessName, ".exe", "" )

  For Each objProcess in colProcesses
  
    If( Err.Number <> 0 ) Then
      checkProcessHandlesWMI  = retvalUnknown
      strSysExplanation      = "Unable to list processes on computer [" & strHost & "]"
      Exit Function 
    End If
    
    If UCase( objProcess.Name )= UCase( strProcessName ) Or UCase( objProcess.Name ) = UCase( strProcessNameShort ) Then
      bProcessFound            = True
 
      nUsageHandles       = CInt( objProcess.HandleCount )
    
      If( nUsageHandles > nHighUsageHandles ) Then
        nHighUsageHandles = nUsageHandles
      End If

    End If
  Next

  If( bProcessFound ) Then
    nDiffHandles             = nMaxHandles - nHighUsageHandles
    strSysData               = nHighUsageHandles
    strSysExplanation        = "Handle Count=[" & nHighUsageHandles & "], maximum allowed=[" & nMaxHandles & "]"
    If nDiffHandles >= 0 then
      checkProcessHandlesWMI    = True
    Else
      checkProcessHandlesWMI    = False
    End if
  Else        
    checkProcessHandlesWMI   = retvalUnknown
    strSysExplanation        = "Process [" & strProcessName & "] is not running on computer [" & strHost & "]"
  End If

End Function 


' //////////////////////////////////////////////////////////////////////////////

Function checkProcessMemoryWMI( objWMIService, strHost, strProcessName, nMemCheckType, nMaxMB, BYREF strSysData, BYREF strSysExplanation )

    Dim objProcess, colProcesses, nUsageMB, nHighUsageMB, nDiff, bProcessFound, strProcessNameShort

    checkProcessMemoryWMI          = retvalUnknown  ' Default return value
    bProcessFound                  = False
    nUsageMB                       = 0
    nHighUsageMB                   = 0
    
On Error Resume Next

  Set colProcesses = objWMIService.ExecQuery( "Select * from Win32_PerfRawData_PerfProc_Process" )
  If( Err.Number <> 0 ) Then
    strSysData         = ""
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( colProcesses.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_PerfRawData_PerfProc_Process class does not exist on computer [" & strHost & "]"
    Exit Function
  End If

On Error Goto 0

  ' Win32_PerfRawData_PerfProc_Process lists processes without .exe, e.g. explorer instead of explorer.exe.
  strProcessNameShort = Replace( strProcessName, ".exe", "" )

  For Each objProcess in colProcesses
  
    If( Err.Number <> 0 ) Then
      checkProcessMemoryWMI  = retvalUnknown
      strSysExplanation      = "Unable to list processes on computer [" & strHost & "]"
      Exit Function 
    End If
    
    If UCase( objProcess.Name )= UCase( strProcessName ) Or UCase( objProcess.Name ) = UCase( strProcessNameShort ) Then
      bProcessFound            = True
      
      ' idxMemPrivateWorkingSet, Task Manager [Private Working Set] -> objProcess.WorkingSetPrivate
      ' idxMemCommitSize, Task Manager [Commit Size] -> objProcess.PrivateBytes
      ' idxMemWorkingSet, Task Manager [Working Set] -> objProcess.WorkingSet
      ' idxMemPeakWorkingSet, Task Manager [Peak Working Set] -> objProcess.WorkingSetPeak
      ' idxMemPrivatePagedPool, Task Manager [Memory - Paged Pool] -> objProcess.PoolPagedBytes
      ' idxMemNonPagedPool, Task Manager [Non-Paged Pool] -> objProcess.PoolNonpagedBytes
      Select Case nMemCheckType
        Case idxMemPrivateWorkingSet
          nUsageMB            = CInt( objProcess.WorkingSetPrivate / ( 1024 * 1024 ) )
        Case idxMemCommitSize
          nUsageMB            = CInt( objProcess.PrivateBytes / ( 1024 * 1024 ) )
        Case idxMemWorkingSet
          nUsageMB            = CInt( objProcess.WorkingSet / ( 1024 * 1024 ) )
        Case idxMemPeakWorkingSet
          nUsageMB            = CInt( objProcess.WorkingSetPeak / ( 1024 * 1024 ) )
        Case idxMemPrivatePagedPool
          nUsageMB            = CInt( objProcess.PoolPagedBytes / ( 1024 * 1024 ) )
        Case idxMemNonPagedPool
          nUsageMB            = CInt( objProcess.PoolNonpagedBytes / ( 1024 * 1024 ) )
        Case Else
          nUsageMB            = CInt( objProcess.PrivateBytes / ( 1024 * 1024 ) )
      End Select
    
      If( nUsageMB > nHighUsageMB ) Then
        nHighUsageMB = nUsageMB
      End If

    End If
  Next

  If( bProcessFound ) Then
    nDiff                    = nMaxMB - nHighUsageMB
    strSysData               = nHighUsageMB
    strSysExplanation        = "Memory usage=[" & nHighUsageMB & "MB], maximum allowed=[" & nMaxMB & " MB]"
    If nDiff >= 0 then
      checkProcessMemoryWMI    = True
    Else
      checkProcessMemoryWMI    = False
    End if
  Else        
    checkProcessMemoryWMI    = retvalUnknown
    strSysExplanation        = "Process [" & strProcessName & "] is not running on computer [" & strHost & "]"
  End If

End Function 

' //////////////////////////////////////////////////////////////////////////////

Function checkAnyProcessMemoryWMI( objWMIService, strHost, nMemCheckType, nMaxMB, BYREF strSysData, BYREF strSysExplanation )

  Dim objProcess, colProcesses, nUsedMB, nDiffMB

  checkAnyProcessMemoryWMI   = retvalUnknown  ' Default return value

On Error Resume Next

  Set colProcesses = objWMIService.ExecQuery( "Select * from Win32_PerfRawData_PerfProc_Process" )
  If( Err.Number <> 0 ) Then
    strSysData         = ""
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( colProcesses.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_PerfRawData_PerfProc_Process class does not exist on computer [" & strHost & "]"
    Exit Function
  End If

On Error Goto 0

  For Each objProcess in colProcesses
    If( Err.Number <> 0 ) Then
      checkAnyProcessMemoryWMI = retvalUnknown
      strSysExplanation        = "Unable to list processes on computer [" & strHost & "]"
      Exit Function 
    End If
    
    ' idxMemPrivateWorkingSet, Task Manager [Private Working Set] -> objProcess.WorkingSetPrivate
    ' idxMemCommitSize, Task Manager [Commit Size] -> objProcess.PrivateBytes
    ' idxMemWorkingSet, Task Manager [Working Set] -> objProcess.WorkingSet
    ' idxMemPeakWorkingSet, Task Manager [Peak Working Set] -> objProcess.WorkingSetPeak
    ' idxMemPrivatePagedPool, Task Manager [Memory - Paged Pool] -> objProcess.PoolPagedBytes
    ' idxMemNonPagedPool, Task Manager [Non-Paged Pool] -> objProcess.PoolNonpagedBytes
    Select Case nMemCheckType
      Case idxMemPrivateWorkingSet
        nUsedMB            = CInt( objProcess.WorkingSetPrivate / ( 1024 * 1024 ) )
      Case idxMemCommitSize
        nUsedMB            = CInt( objProcess.PrivateBytes / ( 1024 * 1024 ) )
      Case idxMemWorkingSet
        nUsedMB            = CInt( objProcess.WorkingSet / ( 1024 * 1024 ) )
      Case idxMemPeakWorkingSet
        nUsedMB            = CInt( objProcess.WorkingSetPeak / ( 1024 * 1024 ) )
      Case idxMemPrivatePagedPool
        nUsedMB            = CInt( objProcess.PoolPagedBytes / ( 1024 * 1024 ) )
      Case idxMemNonPagedPool
        nUsedMB            = CInt( objProcess.PoolNonpagedBytes / ( 1024 * 1024 ) )
      Case Else
        nUsedMB            = CInt( objProcess.PrivateBytes / ( 1024 * 1024 ) )
    End Select    

    nDiffMB                  = nMaxMB -  nUsedMB
    If nDiffMB < 0 then
      strSysData           = nUsedMB
      strSysExplanation    = "Memory usage of process [" & objProcess.Name & "] is [" & nUsedMB & "MB], maximum allowed=[" & nMaxMB & " MB]"
      checkAnyProcessMemoryWMI = False
      Exit Function 
    End If
  Next

  strSysData               = ""
  strSysExplanation        = "No process uses more than [" & nMaxMB & " MB]"
  checkAnyProcessMemoryWMI = True

End Function 


' //////////////////////////////////////////////////////////////////////////////

Function checkProcessCpuWMI( objWMIService, strHost, strProcess, numMaxCpuUsage, BYREF strSysData, BYREF strSysExplanation )

  Dim N1, D1, N2, D2, numLogicalCpus, numCpuUsage
  Dim objProcess, objVbUtils, objInstance1, objInstance2
  Dim strQuery, strProcessEx, strObjectPath

  checkProcessCpuWMI   = retvalUnknown  ' Default return value
  
  numLogicalCpus = getNumLogicalProcessorsWMI( objWMIService, strHost, strSysExplanation ) 
  If( numLogicalCpus <= 0 ) Then
    checkProcessCpuWMI = retvalUnknown
    ' strSysExplanation already set in getNumLogicalProcessorsWMI
    Exit Function
  End If
   
  strProcessEx         = Replace( UCase( strProcess ), ".EXE", "" ) ' Cutt off ".exe" is necessary
  strObjectPath        = "Win32_PerfRawData_PerfProc_Process.Name=" & chr(34) & strProcessEx & chr(34)

  Set objVbUtils       = CreateObject( "ActiveXperts.VbUtilities" )
  
On Error Resume Next  
  Set objInstance1     = objWMIService.get( strObjectPath )
  N1                   = objInstance1.PercentProcessorTime
  D1                   = objInstance1.TimeStamp_Sys100NS
On Error Goto 0
  
  If( N1 = 0 OR D1 = 0 ) Then
    checkProcessCpuWMI = retvalUnknown
    strSysExplanation  = "Process [" & strProcess & "] not running on computer [" & strHost & "]"
    Exit Function
  End If

  objVbUtils.Sleep( 1000 )

On Error Resume Next
  Set objInstance2     = objWMIService.get( strObjectPath )
  N2                   = objInstance2.PercentProcessorTime
  D2                   = objInstance2.TimeStamp_Sys100NS
On Error Goto 0
  
  If( N1 = 0 OR D1 = 0 ) Then
    checkProcessCpuWMI = retvalUnknown
    strSysExplanation  = "Process [" & strProcess & "] not running on computer [" & strHost & "]"
    Exit Function
  End If

  If( ( D2 - D1 ) = 0 ) Then
    checkProcessCpuWMI = True
    strSysData         = "0"
    strSysExplanation  = "CPU usage of process [" & strProcess & "] = [0%]"
    Exit Function
  End If

  numCpuUsage          = CInt( (((N2 - N1) / (D2 - D1)) * 100)/numLogicalCpus )
  strSysData           = numCpuUsage
  strSysExplanation    = "CPU usage of process [" & strProcess & "] = [" & strSysData & "%]" 
  If( numCpuUsage > numMaxCpuUsage ) Then 
    checkProcessCpuWMI = False
  Else 
    checkProcessCpuWMI = True
  End If

End Function

' //////////////////////////////////////////////////////////////////////////////

Function checkNumProcessesWMI( objWMIService, strHost, strProcess, numMinInstances, numMaxInstances, BYREF strSysData, BYREF strSysExplanation )

  Dim objProcess, collProcesses, numProcesses

  checkNumProcessesWMI         = retvalUnknown  ' Default return value
  numProcesses                 = 0

  On Error Resume Next

  set collProcesses = objWMIService.ExecQuery( "Select * from Win32_Process" ) 
  If( Err.Number <> 0 ) Then
    strSysData         = ""
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( collProcesses.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_Process class does not exist on computer [" & strHost & "]"
    Exit Function
  End If

  On Error Goto 0

  For Each objProcess in collProcesses
    If( Err.Number <> 0 ) Then
      checkNumProcessesWMI = retvalUnknown
      strSysExplanation    = "Unable to list processes on computer [" & strHost & "]"
      Exit Function 
    End If
    If UCase( objProcess.Name ) = UCase( strProcess ) Then
      numProcesses = numProcesses + 1
    End If
  Next

  strSysData                  = numProcesses
  strSysExplanation           = numProcesses & " number of instances of process [" & strProcess & "] running on computer [" & strHost & "]"

  If( numProcesses < numMinInstances OR numProcesses > numMaxInstances ) Then
    checkNumProcessesWMI    = False
    Exit Function
  End If

  checkNumProcessesWMI        = True

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getNumLogicalProcessorsWMI( objWMIService, strHost, BYREF strSysExplanation )

  Dim objProcessor, colProcessors, numLogicalProcessors

  getNumLogicalProcessorsWMI   = 0  
  numLogicalProcessors         = 0

On Error Resume Next

  set colProcessors = objWMIService.ExecQuery( "Select * from  Win32_Processor" ) 
  If( Err.Number <> 0 ) Then
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( colProcessors.Count <= 0  ) Then
    strSysExplanation  = "Win32_Processor class does not exist on computer [" & strHost & "]"
    Exit Function
  End If

On Error Goto 0

  For Each objProcessor In colProcessors
    numLogicalProcessors = numLogicalProcessors + objProcessor.NumberOfLogicalProcessors
  Next

  getNumLogicalProcessorsWMI = numLogicalProcessors

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getCredentials( strHost, strAltCredentials, BYREF strAltLogin, BYREF strAltPassword, BYREF strSysExplanation )	

  Dim objNMServerCredentials
  
  strAltLogin = ""
  strAltPassword = ""
  strSysExplanation = ""
  
  getCredentials  = False    
  
  If( strAltCredentials = "" ) Then
    ' No alternate credentials specified, so login and password are empty and service credentials will be used
    getCredentials = True
    Exit Function
  End If
  
  Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" )

  strAltLogin           = objNMServerCredentials.GetLogin( strAltCredentials )
  strAltPassword        = objNMServerCredentials.GetPassword( strAltCredentials )

  If( strAltLogin = "" ) Then
    getCredentials      = False
    strSysExplanation = "No alternate credentials defined for [" & strAltCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials"
    Exit Function
  End If   

  getCredentials = True 

End Function

' //////////////////////////////////////////////////////////////////////////////

Function wmiConnect( strHost, strAltLogin, strAltPassword, BYREF objWMIService, BYREF strSysExplanation )	

  Dim objSWbemLocator, colItems
  Dim bConnectResult

  wmiConnect         = False
  Set objWMIService  = Nothing
      
  If( strAltLogin = "" ) Then	
    ' Connect to remote host on same domain using same security context
On Error Resume Next    
    Set objWMIService     = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strHost &"\root\cimv2" )
    If( Err.Number <> 0 ) Then
      bConnectResult = False
    Else
      bConnectResult = True
    End If
On Error Goto 0    
    
  Else
    ' Connect to remote host using different security context and/or different domain 
On Error Resume Next        
    Set objSWbemLocator   = CreateObject( "WbemScripting.SWbemLocator" )
    Set objWMIService     = objSWbemLocator.ConnectServer( strHost, "root\cimv2", strAltLogin, strAltPassword )

    If( Err.Number <> 0 ) Then
      bConnectResult = False
    Else
      bConnectResult = True
    End If

    objWMIService.Security_.ImpersonationLevel = 3
On Error Goto 0    
    
  End If

  If( Not bConnectResult ) Then
    Set objWMIService  = Nothing
    wmiConnect         = False
    strSysExplanation  = "Failed to connect to '" & strHost & "'. Possible reasons: Login failure, no WMI installed on the remote server, firewall blocking WMI calls, or remote server down"
    Exit Function
  End If    
  
  wmiConnect           = True 
End Function