There are basically two things to address that should make this work for you.
- Get the alert parameters correctly passed to your script.
- Actually call the function defined in your script.
We‘ll start with the parameters. On the Alert Task tab of your alert (pictured above), edit the Task Arguments field and replace: {name}{date}{counter}{threshold}{value}
with: "{name}" "{date}" "{counter}" "{threshold}" "{value}"
Your parameters are basically being parsed as a space-separated string value, so we add double-quotes around each individual parameter token to handle values that include spaces, and we add a space between each individual parameter token so that we‘ll be able to tell one parameter from the next. Then, for the action of your scheduled task (named "Processor Monitoring") you have to tell it to expect parameters from the alert and to pass those parameters to the PowerShell script. Your Action is correct, i.e. "Start a program". For the Program/script field, enter "powershell.exe" (or browse for the full path). And for the Add Arguments field, enter this: -File C:\path\to\your\PowerShell\scripts\perfmon_send_email.ps1 $(Arg0)
Where perfmon_send_email.ps1 is the script file containing your SendMail() function as described above. This bit was kind of finicky, so there may be other ways to set this up, but explicitly using the -File parameter made a difference for my tests. The $(Arg0) part is what gets replaced with the parameter string from the Alert when the scheduled task executes PowerShell to run your script. So that should make the Alert parameters available to your PowerShell script. Now all you have to do is actually call the function you‘ve already defined. Add the following to the end of your script (after the function definition): # Get parameter values by position and pass them to the SendMail() function.SendMail $args[0] $args[1] $args[2] $args[3] $args[4]
$args is an array containing the parameter values passed to a script file called from the command line, which is exactly what we configured the scheduled task to do.
Since we know the alert will always send the same values in the same order, i.e. name, date, counter, threshold, and value, we can just pull them from the command line arguments based on position and pass them to the SendMail() function. Note that there are more robust ways to process command line arguments, but this should be sufficient for your purposes. |