Returning an Exit code from a batch file

by John 10. October 2011 17:16

As you can probably tell by some of these posts, I have been working on automating some of the build-test tasks as it relates to deploying into a testing environment.  Today I found myself wondering how to fail a deployment workflow that has issues inside of a batch file.  As it turns out, this was quite easy...

Inside the batch file, when you detect a problem, you can execute a statement like the following:

exit /b 1

The /b tells the batch file processor that you do not want to exit the full command shell, but rather just termiante the batch file.  The number afterwards is the desired exit or errorlevel code.  A practical example of this functionality would be detecting whether or not the deletion of a file succeeded as follows:

del application.exe
if exist application.exe exit /b 1

The above would return from the batch file with the errorlevel set to 1

Tags: , ,

Detecting whether or not a Web Application Exists from within a batch file

by John 9. October 2011 18:34

In our environment, we use a series of batch files to help automate the creation of a uniform environment when a developer or tester gets a branch of code from TFS.  It has always been annoying to have to open up visual studio to create the various virtual directories, and it is been on my list a long time to clean that up.  This weekend, I had a little down time and decided to investigate just how to do that (Besides this has been on my list for a very long time).

It is probably worth mentioning that the way our environment is configured, developers and testers can all have multiple branches of the code locally on their computers, and they can switch to a new branch just by opening the solution.  Each branch has several solutions that all work together to form the product we are developing.

As part of our internal tooling, I have written a series of utilities that include batch files when appropriate to easily prepare a branch to work as a specific client-environment.  These activities include things like configuring Web Application Directories and project reference paths within the project files, along with a variety of updates to various configuration files to point to the correct WCF Services, Databases, and Directories.

Here is the batch file that works the "magic" of detecting whether or not the Web Application already exists, and if not creates it.

@ECHO OFF
   REM --------------------------------------------------------------------------------
   REM Check for and create WEBAPP under Default Web Site
   REM
   REM %1 is the WEBAPP to create
   REM %2 is the Physical path to the VDIR 
   REM --------------------------------------------------------------------------------

   IF "%1"=="" GOTO BadCommandLine
   IF "%2"=="" GOTO BadCommandLine

   REM Detecting if Web Application Already Exists...
   %windir%\system32\inetsrv\AppCmd.exe list app "Default Web Site/%1" | find "%1" > nul
   IF NOT ERRORLEVEL 1 GOTO AlreadyExists

   ECHO Creating %3 Web Application...
   %windir%\system32\inetsrv\AppCmd.exe ADD app /site.name:"Default Web Site" /path:/%1 /physicalPath:%2 >nul
   GOTO End

:AlreadyExists
   ECHO Virtual Directory already exists.
   GOTO End

:BadCommandLine
   ECHO.
   ECHO Web Application Name and Physical Path Required
   ECHO.
   ECHO CreateVirtualDirectory VirtualDirName PhysicalPath
   ECHO.

:END

 

The above batch file is called like this.

SET WebAppVDIR="WebService"
SET BinDirectory="C:\Projects\Test\test\bin"

REM Create Web Application...
CALL CreateWebApplication %WebAppVDIR% %BinDirectory%

The resulting call will create a Web Application (Using the Default Application Pool) that points to the specified Bin Directory "C:\Projects\Test\test\bin".

Hopefully, this will be useful to someone else. If you have questions or other ideas about how to improve upon this, please leave me a comment.

 

Tags: , ,

About the author

I am a software Entrepreneur, I always have been. I spend my days developing cool new software for several customers, mostly in the Justice space. I have been developing software for clients since I was 14 (I got addicted young). I am always up for a new challenge.

I love the bleeding edge; I have lots of battle scars to prove it.  I spend all of my extra time reading, researching and pushing the limits. Some call me a work-a-holic, to which I respond that there are not enough hours in the day to learn everything I want to know.  Some might say I am forever on a knoweldge quest.

Month List