How tp faild the step when batch script return %errorlevel% otherthen 0

Hi Team,

I am running batch script using octopus rus a script step to deploy the sql DBs using dacpac and I am calling this script from powershell since we dont have option to run batch script directly in this step.
I created a for loop to deploy the dacpac on all required DBs and if any DB fails it will terminate the script, please find the below syntax.

for /f “delims=” %%x in (alldb.txt) do (

“SqlPackage.exe” /Action:Publish /SourceFile:“dacpac” /TargetDatabaseName:"%%x" /TargetServerName:“DB” /TargetUser:“user” /TargetPassword:“password” || echo ERROR && exit /b

so when ever deployment failed for any DB this script is going to stop and when I run echo %errolevel% giving 1, but the same script when I run from powershell this step is showing as successful and octopus email is triggering success email, please find the attached screenshots.

So I ran the same script in powershell directly and then ran echo $? and it returned true, thats why octopus step showing as successful, so how can I make step success or failure depending on this batch script return value.

Please find the screenshot .

Hi @satishy,

Thanks for getting in touch. The key to marketing the step as failed when something goes wrong is to return non-zero from the script or raise an error. Something like if (some error condition) {Exit 1}.

That said, I did some quick testing and I found if you explicitly set an exit code from your script, it should return an non-zero value which is then mapped to the PowerShell $LASTEXITCODE value.

So if you change your batch script to do something like the following.

“SqlPackage.exe” ... || echo ERROR && exit 1 /b

And then check the last exit code in your PowerShell like if ($LASTEXITCODE -ne 0) { Exit 1 }, you should be able to fail the step correctly.

Hope this helps.



Hi Rob,

Thanks for your suggestion, it worked perfectly.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.