Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

msterminalserver.ps1 - powershell script by ActiveXperts Software

msterminalserver.ps1 checks Terminal Server sessions on a host.

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

#################################################################################
# ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script:
#     MsTerminalServer.ps1
# Description: 
#     Check Terminal Server sessions on a (remote) computer. Use network monitor service credentials to access the (remote) computer
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to monitor
#     2) nMaxActiveSessions (int) - Maximum number of Active Sessions allowed. 0 means: do not check Active Sessions
#     3) nMaxInactiveSessions (int) - Maximum number of Inactive Sessions allowed. 0 means: do not check Inactive Sessions
#     4) nMaxTotalSessions (int) - Maximum number of Total Sessions allowed. 0 means: do not check Total Sessions
#     5) strAltCredentials (string, optional) - 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)
# Usage:
#     .\MsTerminalServer.ps1 '<Hostname | IP>' <nMaxActiveSessions> <nMaxInactiveSessions> <nMaxTotalSessions> '[alt-credentials]'
# Sample:
#     .\MsTerminalServer.ps1 'localhost' 0 0 2
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [int]$nMaxActiveSessions = 0, [int]$nMaxInactiveSessions = 0, [int]$nMaxTotalSessions = 0, [string]$strAltCredentials = '' ) 

# -- Use _activexperts.ps1 with common functions 
. 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' 


#################################################################################
# // --- Main script ---
#################################################################################

# -- Clear screen and clear error
cls
$Error.Clear()

# -- Validate parameters, return on parameter mismatch
if( $strHost -eq '' -or $nMaxActiveSessions -lt 0 -or $nMaxInactiveSessions -lt 0 -or $nMaxTotalSessions -lt 0 )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\MsTerminalServer.ps1 "<Hostname | IP>" <nMaxActiveSessions> <nMaxInactiveSessions> <nMaxTotalSessions> "[alt-credentials]"'
  echo $res
  exit
} 

# Declare local variables by assigning an initial value to it
$lstServices = $null
$lstProcesses = $null
$lstPerfCounters = $null

$objAltCredentials = $null
$strExplanation = ''

  
# If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
if( $strAltCredentials -ne '' )
{
  # Get the Alternate Credentials object. Function "AxGetCredentials" is implemented in "activexperts.ps1"
  if( ( AxGetCredentials $strHost  $strAltCredentials ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS )
  {
    echo $strExplanation
    exit
  }
}

# Get WMI object
# Win2003 and older, use: Win32_PerfFormattedData_TermService_TerminalServices
# Win2008, Win7 and higher, use: Win32_PerfRawData_LocalSessionManager_TerminalServices  

$strWmi = 'Win32_PerfRawData_LocalSessionManager_TerminalServices'

if( $objAltCredentials -eq $null )
{
  $objWmi = Get-WmiObject -ComputerName $strHost -Class $strWmi -ErrorVariable Error -ErrorAction SilentlyContinue
}
else
{
  $objWmi = Get-WmiObject -ComputerName $strHost -Class $strWmi -Credential $objAltCredentials -ErrorVariable Error -ErrorAction SilentlyContinue
}
  
if( $objWmi -eq $null )
{
  $res = 'UNCERTAIN: Unable to query WMI on computer [' + $strHost + '].'
  echo $res
  exit
}

# Check for Active Sessions if necessary
if( $nMaxActiveSessions -gt 0 -and $nMaxActiveSessions -lt $objWmi.ActiveSessions )
{
  $res = 'ERROR: Currently ' + $objWmi.ActiveSessions + ' active sessions, Maximum allowed: ' + $nMaxActiveSessions + '.'
  echo $res
  exit
}

# -- Check for Inactive Sessions if necessary
if( $nMaxInactiveSessions -gt 0 -and $nMaxInactiveSessions -lt $objWmi.InactiveSessions )
{
  $res = 'ERROR: Currently ' + $objWmi.InactiveSessions + ' inactive sessions, Maximum allowed: ' + $nMaxInactiveSessions + '.'
  echo $res
  exit
}

# -- Check for Total Sessions if necessary
if( $nMaxTotalSessions -gt 0 -and $nMaxTotalSessions -lt $objWmi.TotalSessions )
{
  $res = 'ERROR: Currently ' + $objWmi.TotalSessions + ' sessions (Total), Maximum allowed: ' + $nMaxTotalSessions + '.'
  echo $res
  exit
}

# -- Print script result
$res = 'SUCCESS: Active Sessions: ' + $objWmi.ActiveSessions + '; Inactive Sessions: ' + $objWmi.InactiveSessions + '; Total Sessions: ' + $objWmi.TotalSessions + '.'
echo $res
exit


#################################################################################
# // --- Catch script exceptions ---
#################################################################################

trap [Exception]
{
  $res = 'UNCERTAIN: ' + $_.Exception.Message
  echo $res
  exit
}