Some strange behavior I noticed with sql server agent not capturing thrown errors from powershell. Take this code:
try{ 1/0 } catch{ Write-Output ($_ | fl -Force | Out-String) throw "failure" }
This will most certainly error, and if you run it in powershell, yep:
Right then, let’s call it from an agent job then via cmd line:
Looks good. Let’s run it!
Uhhh….mkay….let’s check the history of said ‘successful’ job:
I certainly see the exception. Why is the job returning successfully?
The answer?
¯\_(ツ)_/¯
But, I do know how to get it to start throwing errors successfully. The problem is with the –file argument to powershell in the command:
Remove that, and viola:
Job now shows as failed: