Being notified on script errors

I’m just wondering what everyone is doing to get notified when a script fails unexpectedly. Like most of you, I now have a ton of monitoring scripts that run against our assets at various intervals. It only just clicked with me that when a script encounters an exception, it will indicate that on the asset record, but I won’t know about it unless I happen to look at that asset.
I was playing around yesterday, and noticed that if I use “Exit 2” in a catch block, Syncro categories it as a script “failure” and not a script “error” (as happens with “Exit 1”). Failures can be setup in the Notification Center to alert in various ways. Is that the ideal way? How are you doing it?

1 Like

A lot of the time just need to make sure its on the correct power shell version. After that, it really just depends on what you are running. A large number of times, I roll out scripts in groups. Look at the script logs to make sure its reporting the current information then follow up by adding it to more polices. Normally, this method catches a large number of issues with testing scripts and what weird behaviors to look to be corrected with in the code it self. For example try except methods and have it report back any coding errors so I can see it in the log. On the other hand, if I know there could be an error in the future - I might use the same method to report it back in the RMM Alert/ticket there was a script running time error. This is assuming you are testing your code in a test lab for majority of bugs then just need to follow up with weird behavior errors.

Other wise you are at the whim of Syncro at times. For example:
error> C:\ProgramData\Syncro\bin\SomeName.ps1 : File
error> C:\ProgramData\Syncro\bin\SomeName.ps1 cannot be
error> loaded because running scripts is disabled on this system. For more
error> information, see about_Execution_Policies at

In this case, it will always show it running correctly - no error. I think this is a bug on Synco part, but it is what it is.

It would be nice if Syncro would let you get notifications of any general error. Probably would need to be optional, but personally I’d rather know about everything and try to fix them or code around the errors vs not knowing they’re happening. I’ve only accidentally found several scripts with errors in the past that I failed to have enough error catching to capture. And yes, exit 2 works well if you want to get notifications, exit 1 will only show in the asset/audit log. Or you can of course fire off rmm-alerts for errors you catch.

If you use exception handling and fail out of a script directly when something doesn’t work as expected Syncro will pick that up as a failure. If you wanted to throw an RMM alert or something you could do that as well by triggering an RMM alert as part of your exception handling, and then fail the script gracefully.

I was thinking of scripts that run on assets by default (like checking BitLocker status, or Office version). While I always test them thoroughly, there are always edge-cases when you run it against hundreds of assets. Sometimes, a particular configuration is enough to throw an error. In those cases, I had pretty much settled on generating an RMM Alert, but just wanted to see what everyone else was doing.

Thanks for your feedback!

1 Like
> error> C:\ProgramData\Syncro\bin\SomeName.ps1 : File
> error> C:\ProgramData\Syncro\bin\SomeName.ps1 cannot be
> error> loaded because running scripts is disabled on this system. For more
> error> information, see about_Execution_Policies at

The above doesnt get thrown as an error just FYI. The issue is with execution policy being set as Restricted, but as far as I can tell - it does go unnoticed that it has failed.

So looking into this some more, if you want all errors captured and alerted you can add this to the end of your script:

if ($error) {
    Rmm-Alert -Category 'Script Name Error' -Body "$error"
    exit 1

This apparently can even include errors you have hidden with -ErrorAction SilentlyContinue or other ways errors can be hidden, which can be problematic. Not sure if there’s another way that would not do so. If not, you’ll need to add more logic into your script to check things before trying commands that may fail but you don’t particularly care about knowing they failed. Also keep in mind that any exit commands you have above this error catching code will mean no errors get alerted, so this is not a substitute for try/catch and other verify as you go coding.

Exit 1 will show them as red in the activity log, Exit 2 will show as black/failed and generate a notification if you have that option turned on. Doing an alert will show the error, failed notifications do not. Unfortunately Syncro flattens the output to a single line as usual so it can be hard to read and you may end up going to the script output anyway (hence the $error variable to output them all). Just experimenting, we’ll see how much of a pain this is if I put it in all my scripts.

None of the error checking will work if the scripts are just outright not run on schedule. Looking at last nights scripts it seems to be about 50/50 whether the scheduled Thursday night’s scripts ran. No errors- just no longer in the queue and next Thursday now shows as pending. We reboot workstations on Thursdays and there TONS that definitely did not reboot (or run Windows updates per policy) last night.

That’s another issue entirely, start another thread and/or contact support.

Not trying to hijack the thread- I did send into support (no response). Just saying that if you have the correct script failure notification but the script just doesn’t run through no fault of your own it may give you a false sense of security. I also understand that my comment did not help this particular topic I and I apologize.