1

Here's what I'm trying to achieve: I want multiple TFS build configurations, which of which define variable, let's call it foo, which I need to be able to pass as the argument to a PowerShell script which is executed after a successful build. Each build configuration will define a different value for foo.

I know that this is dead simple in TFS 2013 which lets you specify a script to execute after a successful build. Unfortunately I'm using TFS 2012 at the moment and it is not in my gift to upgrade.

The easiest route I could think of to achieve the desired effect is to pass a parameter in the MSBuild Arguments section of the build configuration (/p:foo=myValue ?) and use that parameter in a task in a <Target Name="AfterBuild"> section in the .csproj file for my project.

My question is basically this: what goes in the <Target Name="AfterBuild"> section to invoke the command myscript.ps1 "myValue"?

2 Answers 2

2

This is what I ended up with, for anyone else encountering the same requirement. It will execute a Powershell script included in the build named MyScript.ps1, allowing the build output path to contain spaces, and passing in a parameter set in the TFS build configuration's MS Build Arguments section in the form /p:foo=myValue.

<Target Name="AfterBuild">
    <Exec Command="powershell.exe -NonInteractive -executionpolicy Unrestricted &quot;&amp; '$(TargetDir)MyScript.ps1' '$(foo)'&quot;" />
</Target>
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to be really cool you could add the "execute this after the run" option yourself to your build process in TFS 2012 (maybe it's even portable from the TFS 2013 build templates?). That might be more effort than it's worth for this though.

Anyway to answer your question, you're looking for the Exec task:

<Target Name="AfterBuild">
    <Exec Command="powershell.exe myscript.ps1 $(foo)" />
</Target>

You might have to specify the full path to powershell.exe, I haven't tested this.

2 Comments

Thanks, that was just what I needed. I had to make a couple of changes because the path to the powershell script contained spaces, but this set me on the right path, thanks!
Incidentally, I would have looked into bringing the TFS 2013 behaviour in as you described, but I think we'll be migrating to TFS 2015 in the summer anyway, so I wanted a quick fix until then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.