by Klaus Graefensteiner
1. July 2011 05:28
Introduction
One of the nice TeamCity features is to start builds via http REST API. The TeamCity REST API uses basic authentication and you can provide the Build Type ID and additional data via parameter name value pairs. The perfect command line tool for sending http requests is cURL. cURL wrapped in PowerShell make them an unbeatable duo. This blog post demonstrates how.
Step by Step
Step 1 – Find the build type id
Find the build type id of the build configuration that you would like to start form PowerShell. You just need to open the configuration in the TeamCity portal and look for the bt[number] part of the URL in the browser address bar. In my example it is bt2.

Figure 1: Trigger a build from a PowerShell script
Step 2 – Download cURL
cURL can be downloaded here: cURL
Step 3 – Run the following PowerShell script
Of course you could just run the PowerShell script from the PowerShell ISE or console directly. But double clicking a batch file would be much more convenient.
The first batch file passes an external parameter and the build type id to the second batch file. The second batch file forwards these parameters to the PowerShell script which then calls cURL to trigger a new build.
rerun.bat
run.bat bt3 7734
run.bat
@echo %1
@echo %2
PowerShell -NoExit -file "C:\TeamCityScripts\StartTeamCityBuildWithCurl.ps1" %1 %2
PAUSE
StartTeamCityBuildFromCurl.ps1
Param([string] $BuildTypeId="bt3", [string] $BuildNumber="50")
Set-StrictMode -Version "Latest"
$DebugPreference = "Continue"
$URL = "`"http://localhost:8887/httpAuth/action.html?add2Queue=$BuildTypeId&env.name=ExternalBuildID&env.value=$BuildNumber`""
Write-Debug $URL
Write-Debug "Starting teamcity via cURL and teamcity REST API"
$result = start-process -filePath "C:\TeamCityScripts\curl" `
-ArgumentList "--user", "autoadmin:LuckyYou", $URL `
-NoNewWindow `
-RedirectStandardError "error.txt" `
-RedirectStandardOutput "output.txt" `
-wait `
-passthru
$result.HasExited
$result.ExitCode
Download
The example script can be downloaded here: StartTeamCityBuildFromPowerShell.zip
Ausblick
Automating builds with TeamCity and PowerShell is fun square.