Skip to content

June 8, 2017

SqlAgent not Capturing Errors from Powershell

by Scott Newman

Some strange behavior I noticed with sql server agent not capturing thrown errors from powershell.  Take this code:

    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:


Read more from Powershell, SQL Server

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: