Showing posts with label batch. Show all posts
Showing posts with label batch. Show all posts

Friday, March 9, 2012

Fail Execute Process Task based on batch file ERRORLEVEL?

I am new to this, but have scoured the web and not found an answer to my question...

I have an execute process task that runs a simple batch file. When this batch file completes with an ERRORLEVEL greater than 0, I would like the task to fail. I thought this simply meant setting the "FailTaskIfReturnCodeIsNotSuccessValue" property to true, and setting the SuccessValue to 0. However, this does not appear to work.

Even with a simple batch file forcing the errorcode to 1 as follows, the task still completes "successfully".

SET ERRORLEVEL = 1

Any ideas? Thanks!

have you tried using the script task instead?|||

I am not clear on how I would use the script task to accomplish this, though would welcome suggestions. Also, just for completeness sake, can anyone tell me why a batch file ERRORLEVEL has no effect on the execute process task?

Thanks!

|||

David Kreps wrote:

I am not clear on how I would use the script task to accomplish this, though would welcome suggestions.

you would need to use the System.Diagnostics.Process class within the script task.

|||

The option to fail the task if program result code is not the expected one works with batch file.

The problem is that the command

SET ERRORLEVEL = 1

creates an environment variable ERRORLEVEL, it does not actually affect the batch script error code.

D:>SET ERRORLEVEL = 1
D:>if ERRORLEVEL 1 echo 1
D:>COLOR 00
D:>if ERRORLEVEL 1 echo 1
1

(COLOR 00 is common way to actually set the error level is batch scripts).

|||Perfect. Thank you.|||

Turns out the above did not actually work. While it correctly set the ERRORLEVEL to 1, the package still registered a success. That said, I found the solution: the batch file EXIT command:

EXIT [ERRORLEVEL]

eg: EXIT 1

As the syntax implies, this exits a batch file (or, more accurately, the instance of command.exe it is running in), returning the ERRORLEVEL specified. The Execute Process Task correctly recognizes this ERRORLEVEL as the return code, and fails appropriately.