Fail sql job if error is encountered in powershell script

  • I have a powershell script that has 2 steps. The script is called by a SQL agent job. If a step fails, it is sending back the error to the job but wasn't actually failing the job. I found if I use "throw", it will fail the job. However if the 1st step fails, then it exits the script. I need it to continue on but still show a job failure regardless if step 2 fails or not. Is that possible?

  • How about a try-catch around the first step. Set a variable indicating success or failure, then do step 2, and then throw that variable?

  • Thanks for the idea. I didn't use try-catch since I used -errorvariable in my statement, so instead of calling the throw right after the error occurrs, I waited until the end and checked all errorvariables and called the throw then if they weren't NULL. That was simple. Thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply