Click here to Skip to main content
15,867,756 members
Articles / DevOps / TFS
Tip/Trick

Add PowerShell Scripts to TFS2012 Build

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Dec 2016CPOL3 min read 10.5K   44   3   2
Step by step guide to add pre and post PowerShell scripts to TFS2012 build

Just download the TFS build template and enjoy. :)

Introduction

In this article, we will explain how to customize the TFS2012 build default template to add pre and post PowerShell scripts and create environment variables like the drops path and build number. These features exist by default in TFS2013 and TFS 2015.

Step 1: Edit Build Template

1. Add Workflow Arguments

Open your build's default template workflow "DefaultTemplate.11.1.xaml", then click on Arguments tab and add those four workflow arguments:

  • PostBuildScriptPath
  • PreBuildScriptPath
  • PreBuildScriptArgs
  • PostBuildScriptArgs

Create Hooks Script Section of the Build Process Parameters

We will create a new section "Hook scripts" of the build process parameters that contains the script arguments and their descriptions, and we will add File Browser for PreBuildScriptPath and PostBuildScriptPath arguments to pick script's location from source control.

In the Arguments tab, select the Metadata argument and click on the button "..." in the "Default value" column and add the metadata for the script arguments like below:

To add File Browser for PreBuildScriptPath and PostBuildScriptPath arguments, fill the Editor field with value "Microsoft.TeamFoundation.Build.Controls.ServerFileBrowserEditor, Microsoft.TeamFoundation.Build.Controls"

2. Add Pre-script Workflows

In the workflow, find the sequence called "Compile and Test for Configuration" that does the compilation, then drag an If activity from the toolbox and drop it above the foreach called "For Each Project in BuildSettings.ProjectsToBuild" and set its properties to:

  • DisplayName: If there is a pre-build script

Condition

C#
Not String.IsNullOrEmpty(PreBuildScriptPath)

From the toolbox, drag ConvertWorkspaceItem activity and InvokeProcess activity and drop them in the Then of the "If" activity like the image above.

Right click on the sequence activity that contains your activities and click create variable. Add three variables:

  • preBuildScriptLocalPath (string)
  • postBuildScriptLocalPath (string)
  • scriptResult (Int32)

On the ConvertWorkspaceItem activity, set the following properties:

  • DisplayName: Get pre-build script local path
  • Input: PreBuildScriptPath
  • Result: preBuildScriptLocalPath
  • Workspace: Workspace

Then, in the InvokeProcess activity:

  • Drag a WriteBuildMessage and drop it below stdOutput and set Message property:
    • Message: stdOutput
  • Drag WriteBuildError and drop it below errOutput and set Message property:
    • Message: errOutput

On the InvokeProcess activity, set the following properties:

  • Arguments:
    C#
    String.Format(" ""& '{0}' {1}"" ", preBuildScriptLocalPath, PreBuildScriptArgs)
  • DisplayName: Run pre-build script
  • FileName: "PowerShell"
  • Result: scriptResult

    Now to handle script errors, add an if activity after the InvokeProcess activity and fill its properties with:

  • DisplayName: Check Pre build script errors
  • Condition:
    VB.NET
    scriptResult <> 0

    Then from toolbox, drag Throw component in the "Then" of the If activity and fill its Exception property with:

    VB.NET
    New GenericException("Pre build script failed - please check the log For errors")

This will make the build process stop if the script fails.

Notice That

You have to use in your PowerShell Scripts "exit 0" for success and "exit 1" for unexpected errors.

3. Add Post-script Workflows

Now, let's do the same for the post-build script we will copy the pre-build script "If" activity and paste it below the foreach called "For Each Project in BuildSettings.ProjectsToBuild" and change its properties, the ConvertWorkspaceItem activity and the InvokeProcess activity properties to use post-build variables and arguments.

Step 2: Environment Variables

What if we need some information about the build itself? Like the SourceDirectory and BuildNumber, in TFS2013 and TFS2015, those variables and more are available for you! They are stored as environment variables, but in TFS2012, these variables aren't set so we have to add them.

Let's create an environmental variable called Output_Dir that stores the build output directory, So from the toolbox, drag InvokeMethod activity and drop it above the If activity of your prebuild script and set its properties to:

  • DisplayName: Set build output directory variable
  • MethodName: SetEnvironmentVariable
  • TargetType: System.Environment
  • Parameters:
    • In String "Output_Dir"
    • In String outputDirectory

Now you can use OutPut_Dir variable in your PowerShell script like below:

PowerShell
Param([string]$outDir = $env:Output_Dir)
Notes
  • You should rename the names of your environment variables as same as the names of the environment variables created by TFS2013 and TFS2015 cause that will help when upgrading from TFS2012 so you can use the same PowerShell script.
  • You have to change PowerShell’s execution policy on your build server. Run the following on your build server:
    PowerShell
    Set-ExecutionPolicy RemoteSigned
  • To handle script errors, don't forget to use in your Powershell scripts "exit 0" for success and "exit 1" for errors.

GitHub

If you want to contribute, just check the GitHub repository.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Egypt Egypt
Senior software engineer with experience in Microsoft .net development stack. Having a good knowledge of team- management, solution architecture, and agile environment. I have a great passion for learning new frameworks, tools, and technologies And I do blogging sharing technical articles.

Comments and Discussions

 
QuestionImage error Pin
Mehmet TEMEL3-Dec-16 18:53
Mehmet TEMEL3-Dec-16 18:53 
AnswerRe: Image error Pin
Muhammad Magdi4-Dec-16 3:42
Muhammad Magdi4-Dec-16 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.