Get TeamCity build status from PowerShell

TeamCity has REST api, so it is pretty easy to get the build status from PowerShell.

The function implemented with earliest failure detection: build will be considered as broken if either it has be completed as broken or currently running is broken.

function Test-TeamCityBuildStatus
{
    param
    (
        [string] $ServerUrl,
        [string] $UserName,
        [string] $Password,
        [string] $BuildTypeId
    )

    try
    {
        $client = New-Object System.Net.WebClient
        $client.Credentials = New-Object System.Net.NetworkCredential $UserName, $Password

        $url = "$ServerUrl/httpAuth/app/rest/buildTypes/id:$BuildTypeId/builds/canceled:false/status"

        $status = $client.DownloadString($url)

        if ($status -ne "SUCCESS")
        {
            return $false
        }
        else
        {
            $url = "$ServerUrl/httpAuth/app/rest/buildTypes/id:$buildTypeId/builds/canceled:false,running:any/status"
            $status = $client.DownloadString($url)
            $status -eq "SUCCESS"
        }
    }
    catch
    {
        return $null
    }
}

Leave a comment