Notification on script with error status

Notification on script with error status

avatar
(anonymous user)

Hi.

I want to get notification whenever a script exits with error status. For this purpose I’ve done the following:

  1. Made a script that only contains “$ErrorActionPreference = ‘Stop’“
  2. In my environments, point out the above script in “Startup Scripts”
  3. Made a trigger running a notification script upon “Job Failed”

I thought this would make scripts exiting with status error instead getting status failed, thus triggering the notification. However, my scripts still get status “error” and thus the trigger isn’t triggered. When I put “$ErrorActionPreference = ‘Stop’“ at the top of a script everything works for that script (I get a notification when it fails), just don’t want to do this in each and every script…

Am I going about this the wrong way?

edit: In the script setting ErrorActionPreference I put a line putting something in a log file just to check that it’s really executing, and it is.

All Comments (2)

avatar

What I do is create a trigger for completed jobs. Then the script checks the status of the job ($job.status.value) and sends an alert based on status.

$status = $job.status.value

if ($status -eq 'Error'){
    Send-AlertFunction
}


avatar
What I do is create a trigger for completed jobs. Then the script checks the status of the job ($job.status.value) and sends an alert based on status.

Ah, of course… Yes, that is a much clener solution, thanks!