11.22 RunPowerShell

Use this Knowledge Script to run a non-interactive Microsoft Windows PowerShell cmdlet or PowerShell script (.PS1 file) when an event is raised by another Knowledge Script. You can also use this script to run any command that can be run from a Windows PowerShell command prompt, such as dir c:\temp. PowerShell accepts commands in cmdlet, .PS1, and Windows cmd.exe formats.

The Action_RunPowerShell script makes a number of callback and helper functions available to the PowerShell commands or scripts being run. For more information, see Appendix A, “Using PowerShell Callback and Helper Functions” in the management guide.

11.22.1 Prerequisites

  • Microsoft Windows PowerShell version 1.0 or later

  • Microsoft .NET Framework 3.0 or later

  • AppManager for Windows version 7.6 or later

11.22.2 Setting Parameter Values

Set the following parameters as needed:

Parameter

How to Set It

General Settings

Event Notification

Event Severity - Action failure

Set the event severity level, from 1 to 40, to indicate the importance of an event in which the RunPowerShell job fails. The default is 5.

Severity Configuration

Minimum event severity for Action

Set the minimum severity level, from 1 to 40, for an event that triggers this Action Knowledge Script. The default is 1.

Maximum event severity for Action

Set the maximum severity level, from 1 to 40, for an event that triggers this Action Knowledge Script. The default is 40.

Action

PowerShell command to run

Provide the PowerShell scripts, cmdlets, or code blocks you want to run. Do not provide a command that requires user input. The command should take care of any required input and output redirection or handling. You can string multiple commands together. You can use the following keywords in a command:

  • $ShortMsg$ (short event message)

  • $DetailMsg$ (detailed event message)

  • $Time$ (date and time of the event)

  • $JobID$ (ID of the job that raised the event)

  • $MachineName$ (name of the computer where the event was raised)

  • $Severity$ (severity of the event)

  • $KSName$ (name of the Knowledge Script that raised the event)

  • $ObjectName$ (name of the AppManager resource object where the event was raised)

  • $EventID$ (event ID)

For $ShortMsg$ and $DetailMsg$, you can use number and wildcard options to indicate specific portions of a text string to include. If you do not include an option, AppManager returns the entire text string. For example:

  • $DetailMsg$[5] includes the fifth word of the detailed event message

  • $ShortMsg$[1-5] includes the first through fifth words of the short event message

  • $DetailMsg$[*5] includes the first through fifth words of the detailed event message

  • $ShortMsg$[5*] includes the fifth through last words of the short event message

For example, the following string runs the echo command in PowerShell and prints the detailed event information, starting from the eighth word, into the log.txt file on the agent computer:

Echo $DetailMsg$[8*] > c:\temp\log.txt

Hint If the command you are entering includes quotation marks (“), enclose the quoted string within a second set of quotation marks. For example:

Send-MailMessage -From me@mycompany.com -To you@yourcompany.com -Subject ""Hello!"" -SmtpServer smtp.mycompany.com