Powershell: Queuing Multiple TFS Builds

Problem

I needed to automate a full deployment of all my services, databases and the client that consumed them frequently.

Context

I am using an on premise installation of Team Foundation Server (TFS) 2012 and Visual Studio 2015. I needed to leverage multiple builds since msbuild arguments (the ones that you use to automatically publish/deploy) apply to every project/solution being built. This can potentially lead to multiple different builds.

Solution

Fortunately, this can be accomplished using PowerShell. TFS provides a good useful well-documented interface.


#
# TFS.psm1
#
Function QueueBuilds {
param($serverName, $teamProject, [string[]]$buildDefinitionNames)
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")
$serverName=$serverName
$teamProject=$teamProject
$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
$buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
foreach($buildDefinitionName in $buildDefinitionNames) {
Write-Output "Queuing build $buildDefinitionName"
$buildDefinition = $buildserver.GetBuildDefinition($teamProject, $buildDefinitionName)
$buildRequest = $buildDefinition.CreateBuildRequest();
Write-Output $buildRequest
$result = $buildserver.QueueBuild($BuildReq)
}
}
Export-ModuleMember -Function 'QueueBuilds'

view raw

TFS.psm1

hosted with ❤ by GitHub


#
# Queue Dev Builds
#
Import-Module $PSScriptRoot\TFS\TFS.psm1
$buildNames =
"Service1 – Continuous",
"Service1.DB – Continuous",
"Service2 – Continuous",
"Service2.DB – Continuous",
"Service3 – Continuous",
"Client – Continuous"
QueueBuilds -serverName "http://tfs:8080/tfs/TeamProjectCollection" -teamProject "TeamProject" -buildDefinitionNames $buildNames

Below are some helpful references that I found on my way to the above solution.

Update

I recently started working with a different TFS instance and version only to discover the script produced the below error. I have updated it accordingly.

[ERROR] Multiple ambiguous overloads found for

References

  1. TFS Build API by Example #1: Queue a build.
  2. Queuing TFS Build from Powershell Script Which is Called from TFS Build
  3. Queuing a build in PowerShell
  4. relative path in Import-Module
  5. Powershell import-module doesn’t find modules
  6. How to set a custom tfs build parameter using powershell