Execute Batch File After Tfs Build Template
- Mar 05, 2017 2 Responses to How to run batch file after TFS build Prashant says: February 9, 2014 at 11:43 am Hi Chan, Can you send me xaml template that does this batch file execute i have batch file to be executed on remote server after TFS build and publish website.
- 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.
Transform T4 templates as part of the build, and pass variables from the project. So if you use variables in your template and you want to be able to transform it when you save the template in Visual Studio and when you build the project. 6 thoughts on “Transform T4 templates as part of the build, and pass variables from the project”. Download beamng drive keygen.
I’m building a solution which requires a batch file to be run after the build (there's a sequence in the workflow for this). TFS flags the build as partially succeeded, but there’s no error in the log even in full verbose mode ('diagnostic'). I’m checking the errorlevel after each line in the batch file and it’s always 0. I’ve also tested redirecting stdout and stderr in a file after each line and there’s no clue there.
It’s got nothing to do with unit tests because I’m skipping them for the time being.
I’ve noticed that usually when an error occurs in a batch file (e.g. file not found) there’s a visual cue to indicate the error and this matches the partially succeeded status. But I don’t see any visual cue.
So how can TFS decide that the build is only partially succeeded?
Thank you,

2 Answers
Solved.
It turns out the GetImpactedTests activity is throwing an exception (I can see it in the event viewer of the TFS machine), but it doesn't show at all in the build log.

I'm guessing that this exception makes the build partially succeeded (because the compilation part succeeded) but I couldn't see the assignment explicitly in the buid log. When I bypass the impact analysis (either by setting Analyze Test Impact to False or by removing the GetImpactedTests activity altogether), the error does not occur.
Execute Batch File After Tfs Build Template Configurations To Build
We experiment something similar here using the Lab Workflow (to kick our CodedUI tests). Different build template, same symptoms.
I have noticed that the build process reports that it partially succeeded, highlighting what seems to be a successful step in the deploy script (batch file).
The command is question is a command to install our mobile app on a mobile device (in order to test it at night):
I thought about looking the errorlevel right after running the adb command but the errorlevel was 0.
Then I thought that maybe the command is sending its output to stderr and found out this article on the android open source project, which confirms my hypothesis.
Following is my fix:
Appending 2>&1 simply redirects stderr to stdout and now my deploy script does not report an error anymore and the build now succeeds (when all tests pass!).
Conclusion: When a script writes anything to stderr, the build workflow will report it as an error (partial success since it does not prevent execution of the workflow).
I know this is not your particular issue but since we had the same symptoms, I thought the stderr information could help somebody else find out the reason why their build process is reporting a partial success even though everything seems to work.
Not the answer you're looking for? Browse other questions tagged tfsbatch-filetfsbuild or ask your own question.
Microsoft Visual Studio integrated development environment doesn't run DOS commands, but you can change that fact with a batch file. When IBM introduced PCs, batch files and the original BASIC programming language were among the few ways to write programs. Users became experts at programming DOS commands.
About Batch Files
Batch files might be called scripts or macros in another context. They're just text files filled with DOS commands. For example:
- The '@' suppresses the display of the current statement to the console. So, the command 'ECHO off' is not displayed.
- 'ECHO off' and 'ECHO on' toggles whether statements are displayed. So, after 'ECHO off,' statements are not displayed.
- 'ECHO Hello About Visual Basic!' displays the text 'Hello About Visual Basic!'
- '@ECHO on' switches the ECHO function back on so anything following is displayed.
All of this was just to ensure that the only thing you actually see in the console window is the message.
How to Execute a Batch File in Visual Studio
The key to executing a batch file directly in Visual Studio is to Add one using the External Tools selection of the Tools menu. To do this, you:
- Create a simple batch program that executes other batch programs.
- Reference that program using the External Tools selection in Visual Studio.
To be complete, add a reference to Notepad in the Tools menu.
A Batch Program That Executes Other Batch Programs
Here's the batch program that will execute other batch programs:
The /c parameter carries out the command specified by string and then terminates. The %1 accepts a string that the cmd.exe program will try to execute. If the pause command wasn't there, the command prompt window would close before you could see the result. The pause command issues the string, 'press any key to continue.'
Tip: You can get a fast explanation of any console command—DOS—using this syntax in a command prompt window:
Save this file using any name with the file type '.bat.' You can save it in any location, but the Visual Studio directory in Documents is a good place.
Add an Item to External Tools
The final step is to add an item to the External Tools in Visual Studio.
--------
Click Here to display the illustration
--------
If you simply click the Add button, then you get a complete dialog that allows you to specify every detail possible for an external tool in Visual Studio.
--------
Click Here to display the illustration
--------
In this case, enter the complete path, including the name you used when you saved your batch file earlier, in the Command textbox. For example:
You can enter any name you like in the Title textbox. At this point, your new batch file executing command is ready. Just to be complete, you can also add the RunBat.bat file to the External Tools a different way as shown below:
--------
Click Here to display the illustration
--------
Rather than make this file the default editor in External Tools, which will cause Visual Studio to use RunBat.bat for files that are not batch files, execute the batch file by selecting 'Open With..' from a context menu.
--------
Click Here to display the illustration
--------
Because a batch file is just a text file that is qualified with the .bat type (.cmd works too), you might think that you can use the Text File template in Visual Studio to add one to your project. You can't. As it turns out, a Visual Studio Text File is not a text file. To demonstrate this, right-click the project and use 'Add > New Item .. to add a text file to your project. You have to change the extension so it ends in .bat. Enter the simple DOS command, Dir (display a directory contents) and click OK to add it to your project. If you then try to execute this batch command, you get this error:

That happens because the default source code editor in Visual Studio adds header information to the front of every file. You need an editor, like Notepad, that doesn't. The solution here is to add Notepad to External Tools. Use Notepad to create a batch file. After you save the batch file, you still have to add it to your project as an existing item.