MSBuild Exec Task

Today I had to extend one of our build scripts. We use MSBuild and Cruise Control.  Maybe one day we will get TeamCity; imagine that, a UI. Anyway I wanted to call out to a SQLCMD and unfortunately I could not use the built in SqlCmd task in the MSBuild.Extension.Pack which looks pretty awesome by the way. So I was left with the built-in exec task. Now I haven’t really had a lot to do with the build before and I wasn’t ready for the  myriad of the  xml files that I was confronted with.

This post is for anyone me who is looking for a method of making a SQLCMD call from within MSBuild. If you need to use the out of the box methods this is how to do it. The first command executes SQLCMD in windows authentication mode (because no sql authentication has been supplied) and passes it: the instance and database that it wants to connect to and  the script that it wants to execute. Now you may notice that there is a whole lot of ugly %22 symbols which happen to be ASCII for double quotes. We are simply injecting double quotes into an already double quoted string. Alternative I could have used " the standard XML character entity. either way now you I know. If you prefer a more concise exec string but a more verbose definition of variables you could do the following .