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
}