Example - PowerShell action scripts

The PowerShell action scripts have two instantiated objects available to support successful execution:

Context Variables

The following context variables are available for use in PowerShell action scripts:

Percent Variables

Please see Percent Variables for a list of percent variables that are available for use in PowerShell action scripts.

Important: When using percent variables as part of string literals in your PowerShell scripts, please use double quotation marks (" ") instead of single quotation marks (' ') to enclose the string literal. For example: $Message = "%Device.DisplayName changed state".

Script Timeout

The user can configure a script timeout value (in seconds). If the script has not finished executing before the timeout value expires it will be aborted.

Minimum: 1

Maximum: 60

Default: 10

Example Scripts

Example 1:

#

# This example plays a sound file

#

# Point to an existing wav file

$wavFile = "C:\temp\Sound1.wav"

# Create a .NET SoundPlayer object

$sound = new-Object System.Media.SoundPlayer;

$sound.SoundLocation=$wavFile;

# Play the file

$sound.Play();

# Report the action results. The text will also be logged

$Context.SetResult($result, "Sound action completed")

Example 2:

#

# This example sends an email

#

# Change this value to the recipient

$to = "target_email"

# Change this value to the sender

$from = "source_email"

# This line creates a .NET object for the message

$message = New-Object system.Net.Mail.MailMessage $from, $to

$message.Subject = "Notification from " + $Context.GetProperty("DisplayName")

$message.Body = "Address is down: " + $Context.GetProperty("Address")

# Name the mail server

$server = "alpha.ipswitch.com"

# Create a .NET object to represent the mail client

$client = New-Object System.Net.Mail.SmtpClient $server

$client.UseDefaultCredentials = $true

$result = 1

# Send the message. If no exception is thrown, consider it a success

try {

$client.Send($message);

$result = 0

}

catch {

$result = 1

}

# Report the action results. The text will also be logged

$Context.SetResult($result, "Email Action Completed")

See Also

Configuring an action

Adding and editing an Active Script Action

Adding and editing a Beeper Action

Adding and editing an Email Action

Adding and editing a Log to Text File Action

Adding and editing a Pager Action

Adding and editing a PowerShell action

Adding and editing a Program Action

Adding and editing a Service Restart Action

Adding and editing a SMS Action

Adding and editing a SMS Direct Action

Adding and editing a SNMP Set Action

Adding and editing a Sound Action

Adding and editing a SSH Action

Adding and editing a Syslog Action

Adding and editing a Text-To-Speech Action

Adding and Editing a VMware Action

Adding and Editing a Web Alarm Action

Adding and Editing a Windows Event Log Action

Using the WinPopup Action