Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Author: Tom Kuijsten
Summary: This script can be used to trigger builds in TFS. The only parameter needed is a
comma seperated list of build definitions $BuildsToTrigger.
To trigger the build the REST api of TFS is used. There is one parameter which is passed when
invoking the REST request to trigger the build. This parameter is the <sourceBranch> which should
be set, otherwise my complete source repository is fetched. You might want to add more parameters
depending on your needs.
Note: The user running this script is used to call the REST api and should have enough priviledges
to trigger a build and fetch build information.
$tfsProjectUrl = 'http://<yourserver>/tfs/DefaultCollection/<yourproject>'
function TriggerBuilds()
$definitionsToBuild = GetDefinitionsToTrigger
if($definitionsToBuild.length -eq 0)
Write-Host "##vso[task.logissue type=warning;]There were no builds to trigger."
TriggerBuildsUsingTFS -definitionsToBuild $definitionsToBuild
function GetBuildDefinitions()
$defApiUrl = "$tfsProjectUrl/_apis/build/definitions/"
$buildDefinitionsFromTFS = Invoke-RestMethod $defApiUrl -UseDefaultCredentials
return $buildDefinitionsFromTFS
function GetDefinitionsToTrigger()
$buildNamesToTrigger = $BuildsToTrigger.Split(',') | % { $_.Trim() }
$allDefinitions = GetBuildDefinitions
$definitionsToBuild = @()
foreach($def in $allDefinitions.value)
if($buildNamesToTrigger -icontains $
$definitionsToBuild += $def
return $definitionsToBuild
function GetDefaultBranch($buildDefinition)
$definitionDetails = Invoke-RestMethod $def.url -UseDefaultCredentials
$defaultBranch = $definitionDetails.repository.defaultBranch
return $defaultBranch
function GetJsonPostData($buildId, $defaultBranch)
$definitionIdData = @{'definition'= @{'id'= $buildId}; 'sourceBranch'=$defaultBranch} | ConvertTo-Json
return $definitionIdData
function TriggerBuildsUsingTFS($definitionsToBuild)
foreach($def in $definitionsToBuild)
$buildName = $
$buildId = $
$defaultBranch = GetDefaultBranch -buildDefinition $def
Write-Host "Found build to start $buildName (id=$buildId) on branch $defaultBranch"
Write-Host "Triggering build through REST api..."
$definitionIdData = GetJsonPostData -buildId $buildId -defaultBranch $defaultBranch
$startBuildUrl = "$tfsProjectUrl/_apis/build/builds?api-version=2.0"
$result = Invoke-WebRequest -Uri $startBuildUrl -Method Post -Body $definitionIdData -UseDefaultCredentials -ContentType application/json -UseBasicParsing
$invokeSucceeded = $result.StatusCode -eq 200
Write-Host "... build triggered successfully."
Write-Host "##vso[task.logissue type=warning;]... buid triggered failed."
Write-Host "##vso[task.logissue type=error;]Error occured when triggering build"
Write-Host "##vso[task.logissue type=error;]$_"
#make sure something actually happens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.