VS402903: The specified value is not convertible to type ReleaseDefinition. Make sure it is convertible to type ReleaseDefinition and try again


Recently one of the customer reported that he is trying to update a variable in an existing release definition and it is failing with above mentioned error. Sandeep Venkata from the team worked on this problem and found a good solution which should be useful for you. Here is the customer script, failure and solution.

Script

# Get the existing release definition (have shortened the example to only highlight the problem)
$definitionid = “45”
$uri = “https://$vstsaccount.vsrm.visualstudio.com/defaultcollection/$project/_apis/release/definitions/$definitionid`?api-version=3.0-preview.1”
$content = Invoke-RestMethod -Uri $uri-Method Get -Headers $headers -ContentType “application/json”

 

# Update variables (setting these or not does not affect the error)
$content.variables.EnvironmentId=”5″
$content.variables.Action=”Drop and Create”

 

# Update the release definition
$json= @($content) | ConvertTo-Json
$content2 = Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -ContentType “application/json” -Body $json -Verbose -Debug

 

Failure
Invoke-RestMethod : {“$id”:”1″,”innerException”:null,”message”:”VS402903: The specified value is not convertible to type ReleaseDefinition. Make sure it is convertible to type ReleaseDefinition and try again.”,”typeName”:”Microsoft.V
isualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data”,”typeKey”:”InvalidRequestException”,”errorCode”:0,”eventId”:3000}

 

Solution
On the face of it, the above script looks reasonable and Sandeep found that ConvertTo-Json is not converting the release definition object to JSON format completely and leaving few properties with @ notatation. He observed that by default ConvertTo-Json goes only 2 levels deep and does not convert all the properties of release definition to JSON. The fix here is to specify -Depth parameter with its max value 100 so that the complete definition object is converted to JSON and then it works.
Here is his working powershell script: –

 

$vstsaccount = “<accountName>”
$definitionid = “<id>”
$project = “<projectName>”
$personalAccessToken = “<PAT-Token>”

 

$headers = @{Authorization = ‘Basic ‘ + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(“:$($personalAccessToken)”)) }
$uri = “https://$vstsaccount.vsrm.visualstudio.com/defaultcollection/$project/_apis/release/definitions/$definitionid`?api-version=3.0-preview.2”

 

# GET call to RD
$content = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -ContentType “application/json”
Write-Output $content

 

# Update an existing variable in RD
$content.variables.newVariable.value = “newValue Update of revision ” + $content.revision + ” at $(Get-Date -format s)”
$jsonContent = $content | ConvertTo-Json -Depth 100
$jsonContent = [Text.Encoding]::UTF8.GetBytes($jsonContent)

 

# PUT call to RD
$uri = “https://$vstsaccount.vsrm.visualstudio.com/defaultcollection/$project/_apis/release/definitions/$definitionid`?api-version=3.0-preview.2”
$content2 = Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -ContentType “application/json” -Body $jsonContent -Verbose -Debug
Write-Output $content2

 

Enjoy !!


Comments (0)

Skip to main content