' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // © 1999-2004, ActiveXperts Software B.V.
' //
' // For more information about ActiveXperts Network Monitor and VBScript, please
' // visit the online ActiveXperts Network Monitor VBScript Guidelines at:
' //    http://www.activexperts.com/support/activmonitor/online/vbscript/
' // 
' ///////////////////////////////////////////////////////////////////////////////
'  

Option Explicit
Const retvalUnknown = 1


' //////////////////////////////////////////////////////////////////////////////

Dim strComputer, strService, EXPLANATION

Do
    strComputer = inputbox( "Enter the name of the (remote) computer where the service is registered", "Computer", "localhost" )
Loop until strComputer <> ""

Do
    strService = inputbox( "Enter the name of the service on the (remote) computer", "Service", "AxsNmSvc" )
Loop until strService <> ""

CheckService strComputer, strService
WScript.Echo EXPLANATION



' //////////////////////////////////////////////////////////////////////////////

Function CheckService( strComputer, strService )

' Description: 
'   Checks if a service, specified by strService, is running on the machine specified by strComputer. 
' Parameters:
'   strComputer As String - Hostname or IP address of the computer you want to check
'   strService As String - Name of the service
' Usage:
'   CheckService( "<Hostname or IP address>", "<Service>" )
' Sample:
'   CheckService( "localhost", "alerter" )

On Error Resume Next

    Dim objWMIService

    CheckService      = retvalUnknown  ' Default return value

    Set objWMIService = getWMIObject( strComputer, "", "", EXPLANATION )
    If( objWMIService Is Nothing ) Then
        CheckService  = retvalUnknown
        Exit Function
    End If

    CheckService      = checkServiceWMI( objWMIService, strComputer, strService, EXPLANATION )

End Function

' //////////////////////////////////////////////////////////////////////////////

Function CheckServiceEx( strComputer, strService, strLogin, strPassword ) 

' Description: 
'   Checks if a service, specified by strService, is running on the machine specified by strComputer. Use different credentials to access the (remote) computer
' Parameters:
'   strComputer As String - Hostname or IP address of the computer you want to check
'   strService As String - Name of the service
'   strLogin as String - Use this Windows account
'   strPassword as String - Use this password
' Usage:
'   CheckServiceEx( "<Hostname or IP address>", "<Service>", "<Domain>\<Account>", "<Password>" )
' Sample:
'   CheckServiceEx( "Server01", "alerter", "Server01\Administrator", "topsecret" )

On Error Resume next

    Dim objWMIService

    CheckServiceEx      = retvalUnknown  ' Default return value

    Set objWMIService = getWMIObject( strComputer, strLogin, strPassword, EXPLANATION )
    If( objWMIService Is Nothing ) Then
        CheckServiceEx  = retvalUnknown
        Exit Function
    End If

    CheckServiceEx      = checkServiceWMI( objWMIService, strComputer, strService, EXPLANATION )

End Function



' //////////////////////////////////////////////////////////////////////////////
' //
' // Private Functions
' //   NOTE: Private functions are used by the above functions, and will not
' //         be called directly by the ActiveXperts Network Monitor Service.
' //         Private function names start with a lower case character and will
' //         not be listed in the Network Monitor's function browser.
' //
' //////////////////////////////////////////////////////////////////////////////

Function checkServiceWMI( objWMIService, strComputer, strService, BYREF strExplanation )

On Error Resume Next

    Dim colServices, objService

    checkServiceWMI             = retvalUnknown  ' Default return value

    Set colServices = objWMIService.ExecQuery( "Select * from Win32_Service" )
    If( colServices.Count = 0 ) Then
        checkServiceWMI         = retvalUnknown
        strExplanation          =  "Unable to list services on computer [" & strComputer & "]"
        Exit Function			    
    End If

    For Each objService In colServices
        If( Err.Number <> 0 ) Then
            checkServiceWMI     = retvalUnknown
            strExplanation      = "Unable to list services on computer [" & strComputer & "]"
            Exit Function 
        End If

        If( UCase( objService.Name ) = UCase( strService ) ) Then
            If( objService.State <> "Running" ) Then
                checkServiceWMI = False
                strExplanation  = "Service [" & objService.Name & "] is " & objService.State
                Exit Function
            End If

            checkServiceWMI     = True
            strExplanation      = "Service [" & objService.Name & "] is " & objService.State
            Exit Function			    
        End If
    Next

    checkServiceWMI             = False
    strExplanation              = "Service [" & strService & "] was not found on computer [" & strComputer & "]"
End Function

' //////////////////////////////////////////////////////////////////////////////

Function getWMIObject( strComputer, strUsername, strPassword, BYREF strExplanation )	

On Error Resume Next

    Dim objWMIService, objSWbemLocator, colItems
    
    If( strUsername = "" ) Then	
        ' Connect to remote host on same domain using same security context
        Set objWMIService = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &"\root\cimv2" )
    Else	
        ' Connect to remote host using different security context and/or different domain 
        Set objSWbemLocator = CreateObject( "WbemScripting.SWbemLocator" )
        Set objWMIService = objSWbemLocator.ConnectServer( strComputer, "root\cimv2", strUsername, strPassword )
        If( Err.Number <> 0 ) Then
            getWMIObject = Nothing
            strExplanation = "Unable to access '" & strComputer & "'. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down"
            Exit Function
        End If

        objWMIService.Security_.ImpersonationLevel = 3

    End If
	
    If( Err.Number <> 0 ) Then
        getWMIObject = Nothing
        strExplanation = "Unable to access '" & strComputer & "'. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down"
        Exit Function
    End If    

    Set getWMIObject = objWMIService   

End Function



