0
votes

I am trying to clone all the build definitions in VSTS using powershell REST API method. However, i am facing below error. Sharing the code and error which might be useful.

CODE:

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

}

ERROR:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: definition.Name","typeName":"System.ArgumentNullException, mscorlib, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089","typeKey":"ArgumentNullException","errorCode":0,"eventId":0} At line:42 char:24 + ... wBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Author ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExceptio n + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

1

1 Answers

0
votes

You can prepare the header separately other than that of inline definition

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

}