'How do I fail a SQL Agent job if a Powershell script inside it fails?

I have a Powershell script that has been written to terminate gracefully in the event of an error (ie write the error to a log file and then exit 1.

The issue with this is when it is called inside a SQL Agent job, the job always is marked as success even if the script failed.

What's the best way to use the exit code of the Powershell script to fail the SQL agent job, which then triggers downstream alerting processes?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source