How to Call a batch file from TFS2013 Build?

I run into a situation which needed to run a batch file from TFS Build, this batch file downloads some files from a file server and I just needed to copy that files later during the same build process to another machine.

I will simulate that by creating a batch file that create a file and copy the created file to another machine.

Here is the batch file that create a new text file.
6-22-2014 10-19-33 PM
If I just double click on that file, it will create  a new file called NewFile.txt.
6-18-2014 11-22-07 AM

  1. Open any Build Process Template and delete all its activities.
  2. Add Sequence Activity with name: Overall Build Process.
  3. Drag and drop GetBuildDetail Activity inside the Sequence Activity.
  4. Drag and drop AgentScope Activity with name: Run on Agent.
  5. Drag and drop GetBuildDirectory Activity inside AgentScope Activity.
  6. Drag and drop InvokeProcess Activity with name: Call a batch file.
  7. Declare variable of type IBuildDetail with name: MyBuildDetail,and assign the return value from GetBuildDetail to it.
  8. Declare variable of type String with name: MyBuildDirectory,and assign the return value from GetBuildDirectory to it. More information on how to assign a value to variable from the return execution of activity

6-18-2014 10-33-40 AM

We need to include the folder that contains the script or the batch file in the Source Settings of our Build Definition; so the build server copy it in the working directory of the build.

But first, let’s remember what is the working directory of the build, it’s a folder like a temp folder the build process uses to get the last change set so it could build it, we configure that from TFS Administration Console for the agent.
6-18-2014 11-16-11 AM
So we will need to include the folder that contains the script or the batch file in the Source Settings of our Build Definition.
6-18-2014 11-13-02 AM

So now if we run this build, we will find that it gets the last change set including the script.
6-18-2014 11-17-43 AM

So now I can give the InvokeProcess Activity the path on the working directory as the following.
6-18-2014 11-02-17 AM

Remember if I run the build, the file will not be created in related path, but it will be in System 32 folder
6-18-2014 11-21-38 AM

Then I will need to copy that file to the needed machine, see the following post for typical steps.

How to Copy Build output to a Lab Environment Machine for TFS2013

Share This: