Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

msqueue.ps1 - powershell script by ActiveXperts Software

msqueue.ps1 checks MS Queue length on a host computer.

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


msqueue.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:
#     MsQueue.ps1
# Description: 
#     Checks Ms Queue length on a (remote) computer. 
# Declare Parameters:
#     1) strHost (string)  - Hostname or IP address of the computer you want to monitor
#     3) strQueue (string) - Queue that you want to monitor. Syntax: private$\queuename
#     4) nMaxLength (int) - Maximum allowed items in queue
#     2) strAltCredentials (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)
# Usage:
#     .\MsQueue.ps1 '<Hostname | IP>' '<Queue Name>' <Max_Queue_Length> '[alt-credentials]'
# Sample:
#     .\MsQueue.ps1 'localhost' 'private$\admin_queue$' 10
#################################################################################

# -- Declare Parameters
param ( [string]$strHost = '', [string]$strQueue = '', [int]$nMaxLength = 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 $strQueue -eq '' -or $nMaxLength -lt 0 )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\MsIisServer.ps1 "<Hostname | IP>" "<Queue Name>" <Max_Queue_Length> "[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
$strWmi = 'Win32_PerfRawData_MSMQ_MSMQQueue'

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
}

foreach( $objQueue in $objWmi )
{
  if( $Error.Number -ne 0 )
  {
    $res = "UNCERTAIN: Unable to list queues on computer [" + $strHost + ']'
    echo $res
    exit
  }

  # NOTE: $objQueue.Name is formatted like this: Server01\Private$\AdminQueue$
  # However, we cannot use Server01 because the user may have specified localhost, or 192.168.1.1 as computername
  # So now, filter the queuename by stripping the 
  $nPos = $objQueue.Name.Contains('\')
  $strQueueName = $objQueue.Name.substring( $nPos + 1 )

  if( $strQueueName.ToUpper -eq $strQueue.ToUpper )
  {
    $nQueueLength = $objQueue.MessagesInQueue
    $nQueueBytes = $objQueue.BytesInQueue

    $strSysData = nQueueLength

    if( $nQueueLength -gt $nMaxLength )
    {
      $res = 'ERROR: '
    }
    else
    {
      $res = 'SUCCESS: '
    }
     
    $res += 'Queue length=[' + $nQueueLength + '], maximum allowed=[' + $nMaxLength + ']'
    echo $res
    exit
  }
}
 
# -- Print script result
$res = 'ERROR: Queue [' + $strQueue + '] is not found on computer [' + $strHost + ']'
echo $res
exit


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