Console Task Execution with Flexibility

We may run into a situation where we want to execute a console task within OpsMgr based on some condition, like if a file exists, run it otherwise execute another file. Without the If-condition which is not supported by the <Task>, how can we achieve that? Well there is a workaround. Command line has an IF command through which you can do that.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

It also supports ELSE. However, for it to work, it has to occur on the same line as the command.

IF [NOT] EXIST filename (command) ELSE (command)

Adding the IF statement after the CMD.EXE (or %COMSPEC%) will give you much flexibility executing applications based on some criteria, as opposed to directly running the application.

 

For Example

<

CommandLine>%COMSPEC% /c IF EXIST C:MyFile.exe (C:MyFile.exe) ELSE (C:MyOtherFile.exe)</CommandLine>