Example - PowerShell performance monitor scripts
The PowerShell performance 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 methods are listed below:
- object GetReferenceVariable(string variableName) - allows retrieval of previously configured reference variable values by name.
- 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 performance 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 #1
#
# This example looks for a process named 'outlook' and reports its
# current number of threads.
#
# Use the built-in cmdlet named 'Get-Process', also aliased as 'ps'
$processes = ps
$processName = "outlook"
$proc = $processes | where { $_.ProcessName -match $processName }
# Performance monitors must call Context.SetValue() to report results
$Context.SetValue($proc.Threads.Count)
Example Script #2
#
# This example uses a reference variable to look for idle time
# levels and logs the results
#
# Use available context variables
$resultText = "Address: " + $Context.GetProperty("Address");
# Access the reference variable
$monitorValue = $Context.GetReferenceVariable("IdleTime")
# Log if necessary
$resultText = $resultText + ", Idle time: " + $monitorValue.ToString()
$Logger.WriteLine($resultText)
# Always set the performance value
$Context.SetValue($monitorValue);