Powershell – SQL Server: How to report error from a job’s step of PowerShell type

powershellsql

I have a SQL job whose step of 'PowerShell' type does 'exit 1' if encounters an error i.e.

$ErrorActionPreference='stop'; trap{"$_"; exit 1}

Problem is, SQL Server doesn't pay attention to that exit code, and reports "The step did not generate any output. Process Exit Code 0. The step succeeded."

Any idea how to successfully tell SQL Server from a PowerShell step that something went wrong?

Thanks in advance.

Best Answer

throw "Failure" will work. See this MSDN forum post for additional information: