xenserver-activesessions.ps1 - powershell script by ActiveXperts Software
xenserver-activesessions.ps1 checks the amount of sessions to a XenServer host.
Use xenserver-activesessions.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select xenserver-activesessions.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.
xenserver-activesessions.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
# XenServer-ActiveSessions.ps1
# Description:
# This script checks the amount of sessions of a XenServer server
# Declare Parameters:
# 1) strHost (string) - Hostname or IP address of the computer you want to monitor
# 2) nMaxSessions (int) - Maximum amount of sessions allowed.
# 2) strAltCredentials (string, optional) - Alternate credentials
# Usage:
# .\XenServer-ActiveSessions.ps1 '<Hostname | IP>' <nMaxSessions> '[alt-credentials]'
# Sample:
# .\XenServer-ActiveSessions.ps1 'localhost' 5
# .\XenServer-ActiveSessions.ps1 'remotehost' 5 'remotehost'
#################################################################################
# -- Declare Parameters
param( [string]$strHost = '', [int]$nMaxSessions = -1, [string]$strAltCredentials = '' )
# -- Use _activexperts.ps1 with common functions
. 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1'
# Declare local variables by assigning an initial value to it
$strExplanation = ''
$objAltCredentials = $null
cls
$Error.Clear()
# Check paramters input
if( $strHost -eq '' -or $nMaxSessions -eq -1 )
{
$res = 'UNCERTAIN: Invalid number of parameters - Usage: .\XenServer-ActiveSessions.ps1 "<Hostname | IP>" <nMaxSessions> "[alt-credentials]"'
echo $res
exit
}
# 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
}
}
#################################################################################
#
# // --- Main script ---
#
#################################################################################
if( $Sessions -eq $null )
{
$res = 'SUCCESS: Current Active Sessions: 0'
echo $res
exit
}
$res = 'Current Active Sessions:' + ( $Sessions.Count -1 ) + ' Users:'
$nCurrent = 0;
foreach( $objSession in $Sessions )
{
if( $objSession.Name -ne '_Server Total' )
{
if( $nCurrent -lt 1 )
{
$nBegin = $objSession.Name.IndexOf( '(' )
$nEnd = $objSession.Name.IndexOf( ')' )
$res += ' ' + $objSession.Name.Substring( $nBegin +1, ( $nEnd - $nBegin -1 ) ) + ','
}
else
{
$res += '...'
break
}
$nCurrent += 1
}
}
if( ( $Sessions.Count -1 ) -gt $nMaxSessions )
{
$res = 'ERROR: '
}
else
{
$res = 'SUCCESS: '
}
$res += $res.TrimEnd( ',' ) + ' DATA: ' + ( $Sessions.Count -1 )
echo $res
exit
#################################################################################
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
