2

我有一个 PowerShell 脚本,它调用 TeamCity 的 REST API 进行备份。

这个脚本工作在 v7.1 上,但是当我升级到 8.0.5 时,脚本停止工作。

脚本:

$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

这将失败并显示消息“远程服务器返回错误:(500) 内部服务器错误”。

来自 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)

来自 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: "#########" 

我已经在 Web 浏览器(ntlm 提示符)中测试了脚本运行的凭据,并且 TC 站点加载没有问题。

任何想法我做错了什么?

4

1 回答 1

0

根据文档,您可以使用基本身份验证。

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

这应该可以代替 Execute-HTTPPostCommand。

Invoke-RestMethod -Uri $url -Method Post -UseDefaultCredentials
于 2014-07-29T13:10:46.447 回答