Example - PowerShell active monitor scripts
PowerShell active monitor scripts have two instantiated objects available to support successful execution:
- . An implementation of the IScriptContext interface. This object provides access to runtime variables and also provides mechanism for returning results to the client. A few useful methods are listed below:
- object GetProperty(string propertyName) - allows retrieval of context variable values by name.
- void SetResult(int resultCode) - allows the script to set a value to indicate success, usually 0 = success and 1 = failure.
- . An implementation of the ILog interface. This object provides the same methods available to C# applications. A few useful methods are listed below:
- void Error(string message) - Creates an error-specific log entry that includes the message.
- void Information(string message) - Creates an information-specific log entry that includes the message.
- void WriteLine(string message) - Creates a generic log entry that includes the message.
Context Variables
The following context variables are available for use in PowerShell active monitor scripts:
- DeviceID
- DisplayName
- Address
- NetworkName
- Timeout
- CredWindows:DomainAndUserid
- CredWindows:Password
- CredSnmpV1:ReadCommunity
- CredSnmpV1:WriteCommunity
- CredSnmpV2:ReadCommunity
- CredSnmpV2:WriteCommunity
- CredSnmpV3:AuthPassword
- CredSnmpV3:AuthProtocol (values: 1 = None, 2 = MD5, 3 = SHA)
- CredSnmpV3:EncryptProtocol (values: 1 = None, 2 = DES56, 3 = AES128, 4 = AES192, 5 = AES256, 6 = THREEDES)
- CredSnmpV3:EncryptPassword
- CredSnmpV3:Username
- CredSnmpV3:Context
- CredADO:Password
- CredADO:Username
- CredSSH:Username
- CredSSH:Password
- CredSSH:EnablePassword
- CredSSH:Port
- CredSSH:Timeout
- CredVMware:Username
- CredVMware:Password
Script Timeout
You can configure a script timeout value (in seconds). If the script has not finished executing before the timeout value expires, it aborts.
Minimum: 1
Maximum: 60
Default: 60
Example Script
#
# This example looks for a process named 'outlook' and reports if its
# responding
#
# Use the built-in cmdlet named 'Get-Process', also aliased as 'ps'
$processes = ps
$processName = "outlook"
$proc = $processes | where { $_.ProcessName -match $processName }
# Active monitors must call Context.SetResult() to report results
if ($proc -eq $Null)
{
$NotRunningMessage = "Process '" + $processName + "' is not running."
$Context.SetResult(1, $NotRunningMessage )
}
else
{
if ($proc.Responding)
{
$RespondingMessage = "Process '" + $processName + "' is responding."
$Context.SetResult(0, $RespondingMessage )
}
else
{
$NotRespondingMessage = "Process '" + $processName + "' is not responding."
$Context.SetResult(1, $NotRunningMessage )
}
}