Question

I have a PowerShell script which calls TeamCity's REST API to make a backup.

This script worked was working on v7.1 but when I upgraded to 8.0.5 the script stopped working.

Script:

$ErrorActionPreference = 'Stop'

function Execute-HTTPPostCommand()
 {
    param(
        [string] $url
    )

    $webRequest = [System.Net.WebRequest]::Create($url)
    $webRequest.ContentType = "text/html"
    $PostStr = [System.Text.Encoding]::Default.GetBytes("")
    $webrequest.ContentLength = $PostStr.Length
    $webrequest.AuthenticationLevel = [System.Net.Security.AuthenticationLevel]::MutualAuthRequested
    $webrequest.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    $webRequest.Method = "POST"

    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($PostStr, 0, $PostStr.length)
    $requestStream.Close()

    [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-TeamCityBackup()
 {
    param(
        [string] $server,
        [string] $addTimestamp,
        [string] $includeConfigs,
        [string] $includeDatabase,
        [string] $includeBuildLogs,
        [string] $includePersonalChanges,
        [string] $fileName
    )
    $TeamCityURL = [System.String]::Format("{0}/app/rest/server/backup?addTimestamp={1}&includeConfigs={2}&includeDatabase={3}&includeBuildLogs={4}&includePersonalChanges={5}&fileName={6}",
                                            $server,
                                            $addTimestamp,
                                            $includeConfigs,
                                            $includeDatabase,
                                            $includeBuildLogs,
                                            $includePersonalChanges,
                                            $fileName);
    Write-Host "URL: " $TeamCityURL
    Execute-HTTPPostCommand $TeamCityURL
}


$server = "http://localhost"
$addTimestamp = $true
$includeConfigs = $true
$includeDatabase = $true
$includeBuildLogs = $true
$includePersonalChanges = $true
$fileName = "TeamCity_Backup_"

Execute-TeamCityBackup $server $addTimestamp $includeConfigs $includeDatabase $includeBuildLogs $includePersonalChanges $fileName

This fails with the message "The Remote server returned an error: (500) Internal Server Error".

From teamcity-server.log:

    [2013-12-17 17:53:12,419]  ERROR -   jetbrains.buildServer.SERVER - Error java.lang.IllegalArgumentException: Argument for @NotNull parameter 'key' of jetbrains/buildServer/controllers/interceptors/auth/impl/WaffleBasedNTLMHttpAuthenticationStrategy.getValue must not be null while processing request: POST '/runtimeError.jsp?addTimestamp=True&includeConfigs=True&includeDatabase=True&includePersonalChanges=True&includeBuildLogs=True&fileName=TeamCity_Backup_', from client 0:0:0:0:0:0:0:1:61236, no auth/user 
    java.lang.IllegalArgumentException: Argument for @NotNull parameter 'key' of jetbrains/buildServer/controllers/interceptors/auth/impl/WaffleBasedNTLMHttpAuthenticationStrategy.getValue must not be null
        at jetbrains.buildServer.controllers.interceptors.auth.impl.WaffleBasedNTLMHttpAuthenticationStrategy.getValue(WaffleBasedNTLMHttpAuthenticationStrategy.java)
        at jetbrains.buildServer.controllers.interceptors.auth.impl.WaffleBasedNTLMHttpAuthenticationStrategy.doProcessAuthenticationRequest(WaffleBasedNTLMHttpAuthenticationStrategy.java:57)

From teamcity-auth.log:

[2013-12-17 19:16:44,377]  DEBUG [UA: null ; http-bio-80-exec-28] - Processing request with no authorization header: POST '/app/rest/server/backup?addTimestamp=True&includeConfigs=True&includeDatabase=True&includePersonalChanges=True&includeBuildLogs=True&fileName=TeamCity_Backup_', from client 0:0:0:0:0:0:0:1:55772, no auth/user 
[2013-12-17 19:16:44,377]  DEBUG [UA: null ; http-bio-80-exec-28] - No scheme was matched 
[2013-12-17 19:16:44,377]  DEBUG [UA: null ; http-bio-80-exec-28] - Processing unauthenticated request 
[2013-12-17 19:16:44,377]  DEBUG [UA: null ; http-bio-80-exec-28] - Responding with 401 HTTP status with message "Unauthorized", sending header in response: WWW-Authenticate: Basic realm="TeamCity", Basic realm="TeamCity", NTLM 
[2013-12-17 19:16:44,377]  DEBUG [UA: null ; http-bio-80-exec-26] - Processing request with authorization header: "NTLM": POST '/app/rest/server/backup?addTimestamp=True&includeConfigs=True&includeDatabase=True&includePersonalChanges=True&includeBuildLogs=True&fileName=TeamCity_Backup_', from client 0:0:0:0:0:0:0:1:55773, no auth/user, authorization data: "#########" 

I have tested the credentials the script runs under in a web browser(ntlm prompt) and the TC site loads without issue.

Any ideas what I've done wrong?

Was it helpful?

Solution

According to the documentation you can use basic authentication.

http://confluence.jetbrains.com/display/TCD8/REST+API#RESTAPI-RESTAuthentication

This should work in place of Execute-HTTPPostCommand.

Invoke-RestMethod -Uri $url -Method Post -UseDefaultCredentials
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top