PowerShell Universal Trigger Script Parameters

Hi All,

I am new to this scripting engine and trying to wrap my head around the triggers system. I have an email notification script that I want to be triggered when a job fails or is completed, this script contains a required parameter “recipient” for the recipient of the email notification. When setting this script as a trigger script globally or for another script, I am unable to set the required parameter. When the script is triggered I get a “feedback required” message.
Is there anyway to automate this? Is there a way to set the required parameters when setting the trigger script through the UI or through the New-PSUTrigger command?

Any help here is greatly appreciated,
Many Thanks,
Ryan

Product: PowerShell Universal
Version: 4.4.0

Can you share what your trigger currently looks like so we can see what might be wrong?

Sure!
Here’s my trigger script

param(
    $Job,
   [Parameter(Mandatory, HelpMessage = "Mailboxes to be notified")]
    [ComponentModel.DisplayName("Email addresses")]
    [String[]]$EmailAddresses
    )


$body = @"
Script Ran: $($Job.Script) <br>
Job ID: $($Job.id)<br>
StartTime: $($Job.StartTime)<br>
EndTime: $($Job.EndTime) <br>
Status: $($Job.Status.Value)<br>
See: <a href='admin/automation/jobs/$($Job.id)'>admin/automation/jobs/$($Job.id)</a> <br>
"@

$smtpserver = "mailserver.example.com"
$from = "Service Desk <service.desk@example.com>"
Send-MailMessage -smtpserver $smtpserver -To $EmailAddresses -Subject "Job Failure $($Job.Script)" -BodyAsHTML $body -From $from

When I create a new trigger from the web UI

I notice that if I run the trigger script by itself I am prompted to enter the recipient email address.
However if the email script is triggered. I get a “waiting for feedback” message on the Jobs page.
I’d like to use the same script for multiple Jobs however I’d want to change the recipient depending on the Job being executed.

I hope this makes it clear what I’m trying to achieve.

1 Like

I see. As a trigger script, I’m not sure you can provide the parameter values like that, since it’s being called as part of an automated action and not in a user context.

It’s possible that you could add some logic to your trigger script, however, such as maybe creating a switch statement and conditions that defines a list of email addresses that map to each script you’d want the trigger to be used with, and then have the parameter filled accordingly that way.

For example:

switch ($Job.script)
{
"script1.ps1" {$EmailAddress = 'email1@domain.com'}
"script2.ps1" {$EmailAddress = 'email2@domain.com'}
"script3.ps1" {$EmailAddress = 'email3@domain.com'}
}

Obviously, you’d need to modify that to meet your needs but, that might provide a workaround to this issue since you’ll already know which script should correspond with which email address ahead of time.

1 Like

Thanks for this, Maybe I can manipulate the Job output to return an email address which can be used as the recipient address.
I’ll attempt something along those lines and let you know how I get on.
Thanks again.

1 Like

I wrote a job notification script like this that sends job info and output to an designated email with a html table with colour codes if jobs pass or fail etc.

param ($job)
# $job | Format-List
$Output = Get-PSUJobOutput -JobID $Job.id
$table = [PSCustomObject]@{
    'ID'         = $Job.ID
    'Start Time' = $Job.StartTime
    'End Time'   = $Job.EndTime
    'Script'     = $Job.Script
    'Status'     = ($Job.Status).ToString()
    'Schedule'   = If ($Job.Schedule) {$Job.Schedule} Else {"Not set"}
    'Ran by'     = ($Job.identity.name).ToString()
    'Output'     = $Output
    'Tags'       = $Job.Tags
}

if ($table.Tags -like "*Monitored*") { 

switch($Table.Status) {
Failed {
            $htmltable = $table | ConvertTo-Html -Fragment -Property 'ID', 'Start Time', 'End Time', 'Script', 'Status', 'Schedule', 'Ran by', 'Output', 'Tags' -PreContent "<style>table{font-family: 'Segoe UI', Arial, sans-serif; width: 100%; border-collapse: collapse;} th { background-color: #FF0000; color: black; } td { border: 1px solid #ddd; }</style>"
            $subject = "WARNING: PSU JOB $($Job.ID) FAILURE at $($Job.EndTime)"
            $urlmessage = "`nPlease click here to view the output of the job: https://powershelluniversal.co.uk/admin/automation/jobs/$($job.id)"
            $message = "<br><br><strong><br>The following PSU Job failed, please see details below.<br>.</strong>" + $htmltable + $urlmessage
        }
Error {
            $htmltable = $table | ConvertTo-Html -Fragment -Property 'ID', 'Start Time', 'End Time', 'Script', 'Status', 'Schedule', 'Ran by', 'Output', 'Tags' -PreContent "<style>table{font-family: 'Segoe UI', Arial, sans-serif; width: 100%; border-collapse: collapse;} th { background-color: #FFFF00; color: black; } td { border: 1px solid #ddd; }</style>"
            $subject = "WARNING: PSU JOB $($Job.ID) Completed with errors at $($Job.EndTime)"
            $urlmessage = "`nPlease click here to view the output of the job: https://powershelluniversal.co.uk/admin/automation/jobs/$($job.id)"
            $message = "<br><br><strong><br>The following PSU Job completed with non-terminating errors, please see details below.<br>.</strong>" + $htmltable + $urlmessage
        }
Completed {
            $htmltable = $table | ConvertTo-Html -Fragment -Property 'ID', 'Start Time', 'End Time', 'Script', 'Status', 'Schedule', 'Ran by', 'Output', 'Tags' -PreContent "<style>table{font-family: 'Segoe UI', Arial, sans-serif; width: 100%; border-collapse: collapse;} th { background-color: #00FF00; color: black; } td { border: 1px solid #ddd; }</style>"
            $subject = "PSU JOB $($Job.ID) COMPLETED at $($Job.EndTime)"
            $urlmessage = "`nPlease click here to view the output of the job: https://powershelluniversal.co.uk/admin/automation/jobs/$($job.id)"
            $message = "<br><br><strong><br>The following PSU Job completed, please see details below.<br>.</strong>" + $htmltable + $urlmessage
        }
Default { "Couldn't handle Status of Job"; $table | Format-List ; Exit }}

    try {
        Send-MailMessage -SmtpServer [quote="rali21, post:1, topic:11501, full:true"]
Hi All,

I am new to this scripting engine and trying to wrap my head around the triggers system. I have an email notification script that I want to be triggered when a job fails or is completed, this script contains a required parameter "recipient" for the recipient of the email notification. When setting this script as a trigger script globally or for another script, I am unable to set the required parameter. When the script is triggered I get a "feedback required" message.
Is there anyway to automate this? Is there a way to set the required parameters when setting the trigger script through the UI or through the New-PSUTrigger command?

Any help here is greatly appreciated,
Many Thanks,
Ryan

Product: PowerShell Universal
Version: 4.4.0

[/quote]

[quote="rali21, post:1, topic:11501, full:true"]
Hi All,

I am new to this scripting engine and trying to wrap my head around the triggers system. I have an email notification script that I want to be triggered when a job fails or is completed, this script contains a required parameter "recipient" for the recipient of the email notification. When setting this script as a trigger script globally or for another script, I am unable to set the required parameter. When the script is triggered I get a "feedback required" message.
Is there anyway to automate this? Is there a way to set the required parameters when setting the trigger script through the UI or through the New-PSUTrigger command?

Any help here is greatly appreciated,
Many Thanks,
Ryan

Product: PowerShell Universal
Version: 4.4.0

[/quote]

`Preformatted text`"YOURSMTP" -from "fromemail"  -To "toemail@example.com" -Subject $subject -Body $message -BodyAsHtml -ErrorAction Stop
        Write-Host "The following job is tagged with the 'MONITORED' Tag. An email with its information has been sent."
        $table | Format-List
    }

    Catch { "Failed to send notification email $_" ; $table | Format-List; throw }

}

Else {

    # No email wil be sent as the script is not tagged with the MONITORED Tag in PSU
    Write-Host "The following job is not a monitored job, Only resources tagged with the 'MONITORED' Tag receive notifications"
    $table | Format-List

}
2 Likes

Nice to see a real example of using the switch statement instead of the one I provided that was more hypothetical.

Thanks for this, will definitely give it a try. I like the idea of using Tags to determine which scripts are monitored. I think may be able to set tags with the recipient addresses which I can grab from my trigger script to send the email.
Will keep you updated with the final outcome.

Ended up writing a simple script that gets the mail recipients based on the tags


####################
# Input Parameters
####################

param($Job)

####################
# Constants
####################

$mailRecipientList = ""
$mailSender = ""


####################
# Main
####################

#Check PSUJob for tags
if($Job.tags){

    # Gets mail recipients from tags
    $mailRecipient = $Job.tags.split("|") | where-object { $_ -like "mail:*" }
    if($mailRecipientList){
         # Iterate object and split by semi colon
        $mailRecipientList = $mailRecipient | ForEach-Object({$_.split(":")[1]} )
    }
    # Gets mail recipients from tags
    $mailSender = $Job.tags.split("|") | where-object { $_ -like "from:*" }
    if($mailSender){
        # Iterate object and split by semi colon
        $mailSender = $mailSender | ForEach-Object({$_.split(":")[1]} )
    }

    Send-Notification -PSUJob $Job -to $mailRecipientList -from $mailSender

}else{
    Send-Notification -PSUJob $Job -to "" -from ""
}

The send-notification is a from a custom module that implements logic to send a different html email based on the $Job.Status property, similar to @a.karim 's implementation. The send-notification module also sends the output of the job the the html mail.
Thanks for all the input guys!

1 Like

Awesome. Glad you were able to put something together to solve your problem.

1 Like