使用restapi和powershell创建Team City构建配置

使用restapi和powershell创建Team City构建配置,rest,powershell,teamcity,Rest,Powershell,Teamcity,我正在尝试通过restapi和powershell创建生成配置,并不断出现以下错误: 使用0参数调用GetResponse时出现异常:远程服务器返回错误:405方法不允许 似乎我可以使用GET fine,问题似乎在于PUT命令 代码片段 $url = http://%teamcityServer%:8111/app/rest/buildTypes/id:%projectname%" $req = [System.Net.WebRequest]::Create($url) $req.Content

我正在尝试通过restapi和powershell创建生成配置,并不断出现以下错误:

使用0参数调用GetResponse时出现异常:远程服务器返回错误:405方法不允许

似乎我可以使用GET fine,问题似乎在于PUT命令

代码片段

$url = http://%teamcityServer%:8111/app/rest/buildTypes/id:%projectname%"
$req = [System.Net.WebRequest]::Create($url)
$req.ContentType = "text/plain"
$req.UseDefaultCredentials = $true
$req.Credentials = Get-Credential("username")
$req.Method ="PUT"
$req.ContentLength = 0
$req.Accept = "*/*"
$resp = $req.GetResponse()
$results = [xml]$resp.ReadToEnd()
团队城市日志的输出

2015-09-10 09:14:30582]警告[io-8111-exec-70]-est.jersey.exceptionApperUtil-请求处理405期间发生错误。错误:javax.ws.rs.WebApplicationException。不支持此请求。请检查URL、HTTP方法和传输的数据是否正确。元数据:[允许:[HEAD,DELETE,GET,OPTIONS],]请求:PUT'/app/rest/buildTypes/id:%project%'

TeamCity版本是9.1.1,所以我相信这是可能的


我对restapi相当陌生,因此任何输入都是值得赞赏的。

这是可能的,但您需要发布xml数据来创建构建/项目。例如,如果您需要创建一个项目,您可以发布XML,比如

<newProjectDescription name='New Project Name' id='newProjectId' copyAllAssociatedSettings='true'><parentProject locator='id:project1'/><sourceProject locator='id:project2'/></newProjectDescription>
对于PowerShell 2.0,您可以编写自己的Web请求方法,如:

function Web-Request{   
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Uri,

        [Parameter(Mandatory=$false)]
        [string]
        $Username = $null,

        [Parameter(Mandatory=$false)]
        [string]
        $Password = $null,      

        [Parameter(Mandatory=$true)]
        [string]
        $ContentType,

        [Parameter(Mandatory=$true)]
        [string]
        $Method,

        [Parameter(Mandatory=$false)]
        [string]
        $PostString
    )

    $webRequest = [System.Net.WebRequest]::Create($Uri)
    $webRequest.ContentType = $ContentType  
    $webRequest.Method = $Method
    $webRequest.Accept = "*/*"
    if ($Username)
    {
        $webRequest.Credentials = new-object system.net.networkcredential($Username, $Password)
    }

    try
    {
        switch -regex ($Method)
        {
            "PUT|POST" # PUT and POST behaves similar ways except that POST is only used for creation while PUT for creation/modification   
            {
                if ($PostString -ne "")
                {       
                    $PostStringBytes = [System.Text.Encoding]::UTF8.GetBytes($PostString)
                    $webrequest.ContentLength = $PostStringBytes.Length
                    $requestStream = $webRequest.GetRequestStream()
                    $requestStream.Write($PostStringBytes, 0,$PostStringBytes.length)
                }
                else
                {
                    $requestStream = $webRequest.GetRequestStream()
                }
            }
            "DELETE"
            {
                $requestStream = $webRequest.GetRequestStream()
            }
            default
            {               
                # GET requests usually don't have bodies, default will behave like a GET
            }
        }

        if ($requestStream -ne $null)
        {       
            $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();
    }
    catch
    {
        $results = "Error : $_.Exception.Message"
    }
    finally
    {   
        if ($sr -ne $null) { $sr.Close(); }
        if ($resp -ne $null) { $resp.Close(); }

        $resp = $null;
        $webRequest = $null;
        $sr = $null;
        $requestStream = $null;
    }
    return $results 
}

好的,谢谢,它实际上是PowerShell v2.0。这促使我使用webrequest。我将尝试将其更新到更高版本。我假设$body将是您发布的xml?是的$body将是用于构建/项目创建的xml。我已经更新了答案,并添加了一个Web请求方法来使用PowerShell 2.0。感谢Nadeem,我现在似乎在下面的行[System.Net.WebResponse]$resp=$webRequest.GetResponse;错误:调用带有0个参数的GetResponse时发生异常:远程服务器返回错误:500内部服务器错误..Exception.MessageUpdate:这正在工作,现在。谢谢你的帮助Nadeem
function Web-Request{   
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Uri,

        [Parameter(Mandatory=$false)]
        [string]
        $Username = $null,

        [Parameter(Mandatory=$false)]
        [string]
        $Password = $null,      

        [Parameter(Mandatory=$true)]
        [string]
        $ContentType,

        [Parameter(Mandatory=$true)]
        [string]
        $Method,

        [Parameter(Mandatory=$false)]
        [string]
        $PostString
    )

    $webRequest = [System.Net.WebRequest]::Create($Uri)
    $webRequest.ContentType = $ContentType  
    $webRequest.Method = $Method
    $webRequest.Accept = "*/*"
    if ($Username)
    {
        $webRequest.Credentials = new-object system.net.networkcredential($Username, $Password)
    }

    try
    {
        switch -regex ($Method)
        {
            "PUT|POST" # PUT and POST behaves similar ways except that POST is only used for creation while PUT for creation/modification   
            {
                if ($PostString -ne "")
                {       
                    $PostStringBytes = [System.Text.Encoding]::UTF8.GetBytes($PostString)
                    $webrequest.ContentLength = $PostStringBytes.Length
                    $requestStream = $webRequest.GetRequestStream()
                    $requestStream.Write($PostStringBytes, 0,$PostStringBytes.length)
                }
                else
                {
                    $requestStream = $webRequest.GetRequestStream()
                }
            }
            "DELETE"
            {
                $requestStream = $webRequest.GetRequestStream()
            }
            default
            {               
                # GET requests usually don't have bodies, default will behave like a GET
            }
        }

        if ($requestStream -ne $null)
        {       
            $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();
    }
    catch
    {
        $results = "Error : $_.Exception.Message"
    }
    finally
    {   
        if ($sr -ne $null) { $sr.Close(); }
        if ($resp -ne $null) { $resp.Close(); }

        $resp = $null;
        $webRequest = $null;
        $sr = $null;
        $requestStream = $null;
    }
    return $results 
}