Wednesday, March 23, 2016

Calling Powershell script in TFS 2013 Build Definition

Overview
The Build Definition Default template in TFS 2013 has added the ability to execute Powershell scripts before and after a build (Pre-build and Post-Build). This is different than the pre/post build scripts in Visual Studio. This is much easier than customizing the build template as was required in earlier version of TFS. TFS 2015 is different again, but I believe will still use Powershell scripts for extending its functionality.

Your Powershell Script
You will need to put the Powershell script in a file (a good thing) and reference it in the Pre-build script path or Post-build script path properties of the Process | Build | Advanced screen. There are corresponding Pre-build script agruments and Post-build script arguments properties as well that allow parameters to be passed to the Powershell scripts.

To avoid additional issues or configuration you may want to keep the file in the same path in TFS Source Control. If you don't want or need it to be under source control you can put it directly on the TFS Controller server and reference that path.This can be notably easier for experimenting, but you'll lose the benefits of source control unless manually keep changes in source control.

Logging 
Shows up under the Diagnostics tab (TFS web | your TFS project | Build tab | your build definition | Diagnostics tab)
Write-Host
Write-Output
Write-Warning
Write-Verbose

NOTE: If you want the Write-Verbose calls to show up you need to add [CmdletBinding()] attribute to the beginning of your .ps1 file AND add -verbose to the Pre-Build script agruments property in the build definition.

Environment Variables

  • $env.TF_BUILD_BUILDDEFINITIONNAME
  • $env.TF_BUILD_BUILDDIRECTORY
  • $env.TF_BUILD_BUILDNUMBER
  • $env.TF_BUILD_BUILDREASON
  • $env.TF_BUILD_BUILDURI
  • $env.TF_BUILD_DROPLOCATION
  • $env.TF_BUILD_SOURCEGETVERSION
  • $env.TF_BUILD_SOURCESDIRECTORY
  • $env.TF_BUILD_TESTRESULTSDIRECTORY
You can also put dir anywhere in the Powershell script to see all the environment varialbes.

NOTE: Much of this information is actually gathered from here.
Good information specific to TFS2013 can be found here.

1 comment:

Blogger said...

BlueHost is definitely the best web-hosting company for any hosting services you might need.