Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Fail sql job if error is encountered in powershell script Expand / Collapse
Author
Message
Posted Wednesday, January 15, 2014 9:05 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, March 27, 2014 5:08 AM
Points: 161, Visits: 733
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?
Post #1531175
Posted Wednesday, January 15, 2014 12:22 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 6:36 AM
Points: 402, Visits: 859
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?


Post #1531270
Posted Wednesday, January 15, 2014 3:29 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, March 27, 2014 5:08 AM
Points: 161, Visits: 733
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!
Post #1531338
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse