Start TeamCity build via web request with Powershell script

There are cases when build triggers functionality is not enough. If you don’t want lets say do redundant commits to trigger a build, you can launch a build by web request. Unfortunately I could not find how to do it in official documentation, but after some searching I found a way. You just need to make web request to the following URL: http://YOURSERVER/httpAuth/action.html?add2Queue=btId, where btId is build type Id – unique identifier for each build configuration. To get it, you can just look for it in browser address bar, when clicking on build configuration, or use TeamCity REST  API for details.

Below is Powershell script, which starts TeamCity build by build type Id:

function Execute-HTTPRequestCommand() {
        [string] $url,
        [string] $username,
        [string] $password

    $authInfo = $username + ":" + $password
    $authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($authInfo))

    $webRequest = [System.Net.WebRequest]::Create($url)
    $webRequest.Headers["Authorization"] = "Basic " + $authInfo
    $webRequest.PreAuthenticate = $true

    [System.Net.WebResponse] $resp = $webRequest.GetResponse();
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] $results = $sr.ReadToEnd();

    return $results;

function Execute-TeamCityAdd2Queue() {
        [string] $server,
        [string] $btId
	$TeamCityURL = [System.String]::Format("{0}/httpAuth/action.html?add2Queue={1}", $server, $btId);
	Execute-HTTPRequestCommand $TeamCityURL "USER" "PASSWORD"

$server = "http://YOUR_SERVER"
$btId = "bt5"

Execute-TeamCityAdd2Queue $server $btId


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s