84
votes

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?

4
seems to be true, thanks I'm miss docuemntation information - tbicr
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
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
stackoverflow.com/questions/11096148/… This link uses <Message Importance="high" Text="$(Test2)"/> - Cyrus Downey

4 Answers

152
votes

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

7
votes

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.

6
votes

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>
1
votes

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

echo test output > somefile.txt