Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

msterminalserver.vbs - vbscript script by ActiveXperts Software

msterminalserver.vbs checks Terminal Server sessions on a host.

Use msterminalserver.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select msterminalserver.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.


msterminalserver.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 =  CheckTerminalServer( "localhost", "", 0, 0, 2 )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"

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

Function CheckTerminalServer( strHost, strAltCredentials, nMaxActiveSessions, nMaxInactiveSessions, nMaxTotalSessions ) 

' Description: 
'     Check Terminal Server sessions on a (remote) computer. Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to monitor
'     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) 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( "<Hostname | IP>", "<Empty String | Server>", <MaxActiveSessions>, <MaxInactiveSessions>, <MaxTotalSessions> )
' Sample:
'     CheckTerminalServer( "localhost", "", 0, 0, 2 )

  Dim strAltLogin, strAltPassword, objWMIService

  CheckTerminalServer = retvalUnknown  ' Default return value
  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

  CheckTerminalServer = checkTerminalServerWMI( objWMIService, strHost, nMaxActiveSessions, nMaxInactiveSessions, nMaxTotalSessions, 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 checkTerminalServerWMI( objWMIService, strHost, 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

  ' Win2003 and older, use: Win32_PerfFormattedData_TermService_TerminalServices
  ' Win2008, Win7 and higher, use: Win32_PerfRawData_LocalSessionManager_TerminalServices  
  Set colItems           = objRefresher.AddEnum( objWMIService, "Win32_PerfRawData_LocalSessionManager_TerminalServices").objectSet
  If( Err.Number <> 0 ) Then
    strSysData         = ""
    strSysExplanation  = "Unable to query WMI class on computer [" & strHost & "]"
    Exit Function
  End If
  If( objRefresher.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_PerfRawData_LocalSessionManager_TerminalServices class does not exist on computer [" & strHost & "]"
    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 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