90

I'm trying to get simple output by exec task with msbuild:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Test">
    <Exec Command="echo test output">
      <Output TaskParameter="Outputs" ItemName="Test1" />
    </Exec>
    <Exec Command="echo test output">
      <Output TaskParameter="Outputs" PropertyName="Test2" />
    </Exec>
    <Message Text="----------------------------------------"/>
    <Message Text="@(Test1)"/>
    <Message Text="----------------------------------------"/>
    <Message Text="$(Test2)"/>
    <Message Text="----------------------------------------"/>
  </Target>
</Project>

But get next output:

  echo test output
  test output
  echo test output
  test output
  ----------------------------------------
  ----------------------------------------
  ----------------------------------------

How can I get output by my script?

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
tbicr
  • 24,790
  • 12
  • 81
  • 106
  • seems to be true, thanks I'm miss docuemntation information – tbicr Jan 20 '12 at 09:03
  • 1
    Well documentation is typically not about what isn't possible, but what is. Having that said, your question seems to be common, so maybe you should add appropriate "community content" and thus improve the MSDN documentation. – Christian.K Jan 20 '12 at 09:12
  • 2
    aside from the file hack in the Gathering... related post, it doesn't seem like exec can do this very well with exec. In fact, some people are asking for an improved version in msbuildextensions. What is it you are trying to do with exec? There may be an easier way to accomplish whatever you are attempting. For example, you can get datetime values from the MSBuild.ExtensionPack.Framework.DateAndTime task. If you really need this functionality, I think building a custom msbuild task would be the best route to go. I'll try to whip one up when I have some time and post it on here. – Dan Csharpster Sep 05 '12 at 18:53
  • 1
    http://stackoverflow.com/questions/11096148/msbuild-exec-task-exit-code-empty This link uses – Cyrus Downey Oct 31 '12 at 15:38

4 Answers4

169

Good news everyone! You can now capture output from <Exec> as of .NET 4.5.

Like this:

<Exec ... ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
</Exec>

Simply:

  • Add ConsoleToMsBuild="true" to your <Exec> tag
  • Capture the output using the ConsoleOutput parameter in an <Output> tag

Finally!

Documentation here

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
Avi Cherry
  • 3,996
  • 1
  • 26
  • 31
  • Can you link to the documentation on this, please? – Ark-kun May 31 '13 at 23:23
  • 2
    Oops, ya. It's at: http://msdn.microsoft.com/en-us/library/ms124731(v=VS.110).aspx – Avi Cherry Jun 01 '13 at 23:27
  • 2
    This works with MSBuild 12, which is installed by the Microsoft Build Tools 2013: https://www.microsoft.com/en-us/download/details.aspx?id=40760 – alexandrul May 22 '15 at 10:15
  • 1
    @AviCherry can you please include the link to the documentation in your answer rather than in the comments? Were you able to get this to work in the context of TFS Team Build 2012? – Ryan Gates Sep 28 '15 at 21:02
  • Updated. No, I haven't tried it in the context of TFS. – Avi Cherry Sep 28 '15 at 21:35
  • 3
    Great find, @AviCherry! What an oversight that ConsoleToMSBuild ConsoleOutput are missing from the documentation for the Exec task itself (https://msdn.microsoft.com/en-us/library/x8zx72cd.aspx). – weir Nov 03 '15 at 12:12
  • @RyanGates- This should be available on a Team Build 2012 server provided either Microsoft Build Tools 2013 (or later) or Visual Studio 2013 (or later) is installed on the server. – weir Nov 03 '15 at 12:14
  • Thanks! Is it possible to just redirect the output to variable and not write anything to console output? – pfedotovsky Aug 05 '16 at 15:43
  • 1
    Plus 1 for the Futurama reference. Also, for the great solution ;) – Ohad Schneider Jun 08 '17 at 08:58
  • 1
    This is now documented here: https://learn.microsoft.com/en-us/visualstudio/msbuild/exec-task – Raif Atef Feb 11 '18 at 13:43
  • 1
    Also note that by default the console output will still appear in standard out. If you want to hide it, set `StandardOutputImportance="Low"`. – Matt Johnson-Pint Jan 04 '23 at 20:39
9

If you want to capture output to an array-like structure and not to a plain string where the output lines are separated by a semicolon, use ItemName instead of PropertyName:

<Exec ... ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" ItemName="OutputOfExec" />
</Exec>
6

I've gotten to the point where I'm so frustrated with the limitations of MSBuild, and the stuff that's supposed to work but doesn't (at least not in every context), that pretty much anytime I need to do anything with MSBuild, I create a custom build task in C#.

If none of the other suggestions are working, then you could certainly do it that way.

Samer Adra
  • 683
  • 7
  • 16
  • 4
    I feel your pain - though my custom task is a python script. – NetworkBurger Feb 12 '14 at 15:43
  • Understandable but I think MSBuild is more of a structured way to pipe information into the output/error consoles. Although I definitely feel that it's a step above batch files – Nate-Wilkins Feb 19 '14 at 17:39
  • 2
    I know this is waaaay down the track from when you had this issue, but I just had the same thing happen and resolved it by setting ToolsVersion="12.0" in the root element.. hope this is of some help :) – Paul Carroll May 22 '14 at 22:03
0

You can pipe the output to a file so to speak, and read it back.

echo test output > somefile.txt
Syam
  • 1,629
  • 1
  • 20
  • 32