performancecounter.ps1 - powershell script by ActiveXperts Software
performancecounter.ps1 checks one or more Windows Performance Counters on a host.
Use performancecounter.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select performancecounter.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.
performancecounter.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
# PerformanceCounter.ps1
# Description:
# This function uses ActiveXperts 'NMPerf' ActiveX object.
# Declare Parameters:
# 1) strHost (string) - Hostname or IP address of the computer you want to monitor
# 2) strCounter (string) - Performance counter
# 3) strContext (string) - Performance context. Specify an empty string in case there's no context required
# 4) strItem (string) - Performance item.
# 5) strOperator - Operator, used to compare current value against the limit. Valid values are =, <, <=, <>, >, >=
# 6) nValue (int) - Limit
# 7) strAltCredentials (string) - pecify 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:
# .\PerformanceCounter.ps1 '<Hostname | IP>' '<Counter>' '<Context>' '<Item>' '<Operator>' <nValue> '[alt-credentials]'
# Sample:
# .\PerformanceCounter.ps1 'localhost' 'Processor' '_Total' '% Processor Time' '-lt' 2
#################################################################################
# -- Declare Parameters
param( [string]$strHost = '', [string]$strCounter = '', [string]$strContext = '', [string]$strItem = '', [string]$strOperator = '', [int]$nValue = 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 $strCounter -eq '' -or $strContext -eq '' -or $strItem -eq '' -or $strOperator -eq '' -or $nValue -lt 0 )
{
$res = 'UNCERTAIN: Parameter error - Usage: .\PerformanceCounter.ps1 "<Hostname | IP>" "<Counter>" "<Context>" "<Item>" "<Operator>" <nValue> "[alt-credentials]"'
echo $res
exit
}
# -- Check for valid operator
if( (AxIsOperatorValid( $strOperator ) ) -ne 1 )
{
$res = 'UNCERTAIN: Invalid Operator specified. Valid Operators are (-eq, -lt, -gt, -ge, -le, -ne).'
echo $res
exit
}
# -- Declare local variables by assigning an initial value to it
$strExplanation = ''
$objAltCredentials = $null
# 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
}
}
$lstPerfCounters = @( ( $strCounter, $strContext, $strItem, ( $strOperator + " " + $nValue ) ),
( '', '', '', '' ) )
$dummy = AxCheckPerfCounters $strHost ([ref]$lstPerfCounters) $objAltCredentials ([ref]$strExplanation)
# -- Print script result
echo $strExplanation
exit
#################################################################################
# // --- Catch script exceptions ---
#################################################################################
<#
trap [Exception]
{
$res = 'UNCERTAIN: ' + $_.Exception.Message
echo $res
exit
}
#>
