Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

process-count.ps1 - powershell script by ActiveXperts Software

process-count.ps1 counts the number of processes.

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


process-count.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:
#     Process.ps1
# Description:
#     This function counts the number processes - indicated by strProcess -on the computer specified by strHost.
#     If this count is less than numMinInstances or greater than numMaxInstances, an error is generated.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP
#     2) strProcess (string) - Name of the process to check
#     3) numMinInstances (int) - minimum number of instances
#     4) numMaxInstances (int) - maximum number of instances
#     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:
#     .\Process.ps1 '<Hostname | IP>' '<Process Name>' <numMinInstances> <numMaxInstances>'[alt-credentials]'
# Sample:
#     .\Process.ps1 'localhost' 'svchost.exe' 1 5
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strProcess = '', [int]$numMinInstances = 0, [int]$numMaxInstances = 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 $strProcess -eq '' )
{
  echo 'UNCERTAIN: Invalid number of parameters - Usage: .\Process.ps1 "<Hostname | IP>" "<Process Name>" <numMinInstances> <numMaxInstances> "[alt-credentials]"'
  exit
}

# Declare local variables by assigning an initial value to it
$lstProcesses = $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
  }
}


if( $objAltCredentials -eq $null )
{
  $objWmi = Get-WmiObject -ComputerName $strHost -Class Win32_Process -ErrorVariable Error -ErrorAction SilentlyContinue
}
else
{
  $objWmi = Get-WmiObject -ComputerName $strHost -Class Win32_Process -Credential $objAltCredentials -ErrorVariable Error -ErrorAction SilentlyContinue
}

if( $Error -ne '' )
{
  $strExplanation.value = 'UNCERTAIN: ' + $Error
  return $AXUNCERTAIN
}

$numProcesses = 0


$bCheck = $true
if( $strProcess.StartsWith('!') )
{
  $bCheck = $false
}

if( -not $bCheck ) { continue; }

# Check if process exists
$objWmiNameList = ( $objWmi | select name )

$bFound = $false
foreach( $processName in $objWmiNameList )
{
  if( $processName -match $strProcess )
  {
    $bFound = $true
    $numProcesses += 1
  }
}

if( -not $bFound )
{
  $res = 'ERROR: Process [' + $strProcess + '] is not running on [' + $strHost + ']'
  echo $res
  exit
}


if( ( $numProcesses -lt $numMinInstances )-or( $numProcesses -gt $numMaxInstances ) )
{
  $res = 'ERROR: ' + $numProcesses + ' instances of process [' + $strProcess + '] running on computer [' + $strHost + ']'
  echo $res
  exit
}

# -- Print script result
$res = 'SUCCESS: ' + $numProcesses + ' instances of process [' + $strProcess + '] running on computer [' + $strHost + ']'
echo $res
exit


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

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