Contact Info

Crumbtrail

ActiveXperts.com » Administration » VBScript » Network Monitor » Terminal Server

MsTerminalServer.vbs - Microsoft Terminal Server monitoring using ActiveXperts Network Monitor

ActiveXperts Network Monitor ships with a powerful set of pre-defined checks. Each individual check has a static number of configuration items. To monitor other items, or to combine monitoring items, you can make use of custom VBScript checks.

Most of the built-in checks have a VBScript equivalent, implemented as a Function in a VBScript (.vbs) file. Out-of-the-box, each VBScript function monitors the same items as the built-in check. Feel free to modify a function. The VBScript check can be customized by editing the VBScript function.

To add a new VBScript-based Microsoft Terminal Server monitoring check, do the following:

To customize the above monitoring check, click on the 'Edit button' next to the 'File selection box'. Notepad will be launched. You can now make changes to the VBScript function(s).

Screenshot of a VBScript MsTerminalServer check

MsTerminalServer.vbs script source code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' //  ActiveXperts Software B.V.
' //
' // For more information about ActiveXperts Network Monitor and VBScript, please
' // visit the online ActiveXperts Network Monitor VBScript Guidelines at:
' //    https://www.activexperts.com/support/network-monitor/online/vbscript/
' // 
' ///////////////////////////////////////////////////////////////////////////////
'  

Option Explicit
Const  retvalUnknown = 1
Dim    SYSDATA, SYSEXPLANATION  ' Used by Network Monitor, don't change the names


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

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

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

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

Function CheckTerminalServer( strComputer, strCredentials, nMaxActiveSessions, nMaxInactiveSessions, nMaxTotalSessions ) 

' Description: 
'     Check Terminal Server sessions on a (remote) computer. Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strComputer As String - Hostname or IP address of the computer you want to monitor
'     2) strCredentials 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) nMaxActiveSessions As Number   - Maximum number of Active Sessions allowed. 0 means: do not check Active Sessions
'     4) nMaxInactiveSessions As Number - Maximum number of Inactive Sessions allowed. 0 means: do not check Inactive Sessions
'     5) nMaxTotalSessions As Number    - Maximum number of Total Sessions allowed. 0 means: do not check Total Sessions
' Usage:
'     CheckTerminalServer( "", "", MaxActiveSessions, MaxInactiveSessions, MaxTotalSessions )
' Sample:
'     CheckTerminalServer( "localhost", "", 0, 0, 2 )

    Dim objWMIService

    CheckTerminalServer = retvalUnknown  ' Default return value
    SYSDATA             = ""             ' Used to store session information
    SYSEXPLANATION      = ""             ' Set initial value

    If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
        Exit Function
    End If


    CheckTerminalServer = checkTerminalServerWMI( objWMIService, strComputer, nMaxActiveSessions, nMaxInactiveSessions, nMaxTotalSessions, SYSDATA, SYSEXPLANATION )

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 checkTerminalServerWMI( objWMIService, strComputer, nMaxActiveSessions, nMaxInactiveSessions, nMaxTotalSessions, BYREF strSysData, BYREF strSysExplanation ) 


    Dim objRefresher, objUtils, objItem
    Dim colItems
    Dim nActiveSessions, nInactiveSessions, nTotalSessions
    
    checkTerminalServerWMI = retvalUnknown ' Default return value

    set objRefresher       = CreateObject("WbemScripting.SWbemRefresher")
    Set objUtils           = CreateObject( "ActiveXperts.VbDebugger" )

    nActiveSessions        = -1
    nInactiveSessions      = -1
    nTotalSessions         = -1

On Error Resume Next

    Set colItems           = objRefresher.AddEnum( objWMIService, "Win32_PerfFormattedData_TermService_TerminalServices").objectSet
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( objRefresher.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "Win32_PerfFormattedData_TermService_TerminalServices class does not exist on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    objRefresher.Refresh
    objUtils.Sleep 2000
    objRefresher.Refresh

    For Each objItem in colItems
        nActiveSessions    = objItem.ActiveSessions
        nInactiveSessions  = objItem.InactiveSessions
        nTotalSessions     = objItem.TotalSessions
    Next

    If( nActiveSessions    = -1 OR nInactiveSessions = -1 OR nTotalSessions = -1 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to retrieve session counters"
        checkTerminalServerWMI = retvalUnknown
        Exit Function
    End If

    ' Set Data now
    strSysData             = nActiveSessions 
    strSysData             = strSysData & "," 
    strSysData             = strSysData & nInactiveSessions
    strSysData             = strSysData & "," 
    strSysData             = strSysData & nTotalSessions

    ' Check for Active Sessions if necessary
    If( nMaxActiveSessions > 0 AND nActiveSessions > nMaxActiveSessions ) Then
        strSysExplanation      = "Currently " & nActiveSessions & " active sessions. Maximum allowed: " & nMaxActiveSessions & "."
        checkTerminalServerWMI = False
        Exit Function
    End If
       
    ' Check for Inactive Sessions if necessary
    If( nMaxInactiveSessions > 0 AND nInactiveSessions > nMaxInactiveSessions ) Then
        strSysExplanation      = "Currently " & nInactiveSessions & " inactive sessions. Maximum allowed: " & nMaxInactiveSessions & "."
        checkTerminalServerWMI = False
        Exit Function
    End If

    ' Check for Total Sessions if necessary
    If( nMaxTotalSessions > 0 AND nTotalSessions > nMaxTotalSessions ) Then
        strSysExplanation      = "Currently " & nTotalSessions & " sessions (total). Maximum allowed: " & nMaxTotalSessions & "."
        checkTerminalServerWMI = False
        Exit Function
    End If
       

   checkTerminalServerWMI      = True
   strSysExplanation           = "Active Sessions: " & nActiveSessions & "; Inactive Sessions: " & nInactiveSessions & "; Total Sessions: " & nTotalSessions & "."

End Function


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

Function getWMIObject( strComputer, strCredentials, BYREF objWMIService, BYREF strSysExplanation )	

On Error Resume Next

    Dim objNMServerCredentials, objSWbemLocator, colItems
    Dim strUsername, strPassword

    getWMIObject              = False

    Set objWMIService         = Nothing
    
    If( strCredentials = "" ) Then	
        ' Connect to remote host on same domain using same security context
        Set objWMIService     = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &"\root\cimv2" )
    Else
        Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" )

        strUsername           = objNMServerCredentials.GetLogin( strCredentials )
        strPassword           = objNMServerCredentials.GetPassword( strCredentials )

        If( strUsername = "" ) Then
            getWMIObject      = False
            strSysExplanation = "No alternate credentials defined for [" & strCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials"
            Exit Function
        End If
	
        ' 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
            objWMIService     = Nothing
            getWMIObject      = False
            strSysExplanation = "Unable to access [" & strComputer & "]. Possible reasons: WMI not running on the remote server, Windows firewall is blocking WMI calls, insufficient rights, or remote server down"
            Exit Function
        End If

        objWMIService.Security_.ImpersonationLevel = 3

    End If
	
    If( Err.Number <> 0 ) Then
        objWMIService         = Nothing
        getWMIObject          = False
        strSysExplanation     = "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    

    getWMIObject              = True 

End Function