7

CmdExec vs Powershell – error? What error?

 3 years ago
source link: https://thelonedba.wordpress.com/2020/10/20/cmdexec-vs-powershell-error-what-error/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CmdExec vs Powershell – error? What error?

Just a quickie Batch file scripts, called by SQL Agent, are not propagating their errors back up to the SQL Server job system because…. they’re being called from a Powershell script?

To replicate the problem, create a .bat file containing the following, being a quick way of generating an error response:

exit /b 1

Then create a SQLAgent job which calls this script as an “Operating System (CmdExec)” job step, and a second step of doing something dull, such as

select 1

Set the first job step to go to the second job step in the event of a failure. I’ve set up two steps, one is just the exit command above, the second calls that script instead using cmd /C

cmd /C "exittest.bat"

Launch the job. The job succeeds, but shows in history with a warning:

error-handling-cmd-vs-posh.png?w=754
both options give an error / warning combo

So, how do we fix this? My suspicion is that we can’t, as powershell would be looking at the error generated / returned by cmd.exe, as that is (I’m guessing) what’s invoked when you launch a .bat file inside a powershell script. If you know otherwise, please let me know!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK