使用REST API克隆构建定义时出错

时间:2019-05-14 05:51:06

标签: powershell

我正在尝试使用powershell REST API方法在VSTS中克隆所有构建定义。但是,我面临以下错误。共享可能有用的代码和错误。

代码:

Clear-Host
$buildToCloneName = $buildWeWant
$newBuildName = $buildWeWant-Clone

$user = "xxxxxxx"
$accessToken="xxxxxxxx"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$accessToken)))
$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = "https://xxxxx.visualstudio.com/"
$env:SYSTEM_TEAMPROJECTID = "xxxxxxx"

"Getting all bulid definitions"
$allSuitesBuildUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
$allSuitedBuilds = Invoke-RestMethod -Uri $allSuitesBuildUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}


foreach($buildDetails in $allSuitedBuilds.value){

    $buildWeWant = $buildDetails
    $buildId = $buildWeWant."id"


    [int]$buildIdTest = $null
    if(![int]::TryParse($buildId, [ref]$buildIdTest))
        {
            throw [Exception] "ERROR: NO BUILD ID FOUND"
        }

        "Getting the exact definition for the build"
        # You can see this in the browser using xxxxxxxxxx
        $thisBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions/" + $buildId + "?api-version=2.0"
        $thisBuildDefUrl
        $thisBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

        ## Create a name for the clone by prefixing "_Clone" to the build name
        "Assigning a new name"
        $thisBuildDef.Name = $buildWeWant."id"."_Clone" 


        "Creating a clone build with name $newBuildName"
        $defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100
        $newBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
        $newBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop


        $newBuildDefAsJson = $newBuildDef | ConvertTo-Json -Depth 100
        $newBuildDefAsJson

        "New Build Created"
        $newBuildDef.Name

}

错误:

  

Invoke-RestMethod:{“ $ id”:“ 1”,“ innerException”:null,“ message”:“ Value   不能为null。\ r \ n参数名称:   definition.Name“,” typeName“:” System.ArgumentNullException,mscorlib,   版本= 14.0.0.0,文化=中性,   PublicKeyToken = b77a5c561934e089“,” typeKey“:” ArgumentNullException“,” errorCode“:0,” eventId“:0}   在线:42字符:24   + ... wBuildDef = Invoke-RestMethod -Uri $ thisBuildDefUrl -Headers @ {作者...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~       + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-RestMethod],   网络体验       + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

1 个答案:

答案 0 :(得分:0)

除了内联定义的标题外,您还可以单独准备标题

Clear-Host
$buildToCloneName = $buildWeWant
$newBuildName = $buildWeWant-Clone

$user = "xxxxxxx"
$accessToken = "xxxxxxxx"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $accessToken)))
$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = "https://xxxxx.visualstudio.com/"
$env:SYSTEM_TEAMPROJECTID = "xxxxxxx"

"Getting all bulid definitions"
$allSuitesBuildUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
$allSuitedBuilds = Invoke-RestMethod -Uri $allSuitesBuildUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo) }


foreach ($buildDetails in $allSuitedBuilds.value) {

    $buildWeWant = $buildDetails
    $buildId = $buildWeWant."id"


    [int]$buildIdTest = $null
    if (![int]::TryParse($buildId, [ref]$buildIdTest)) {
        throw [Exception] "ERROR: NO BUILD ID FOUND"
    }

    "Getting the exact definition for the build"
    # You can see this in the browser using xxxxxxxxxx
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", "Basic $base64AuthInfo")
    $headers.Add("Content-Type", "application/json")

    $thisBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions/" + $buildId + "?api-version=2.0"
    $thisBuildDefUrl
    $thisBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers $headers

    ## Create a name for the clone by prefixing "_Clone" to the build name
    "Assigning a new name"
    $thisBuildDef.Name = $buildWeWant."id"."_Clone"


    "Creating a clone build with name $newBuildName"
    $defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100

    $newBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
    $newBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers $headers -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop


    $newBuildDefAsJson = $newBuildDef | ConvertTo-Json -Depth 100
    $newBuildDefAsJson

    "New Build Created"
    $newBuildDef.Name

}