1

I am trying to invoke VSTS release API using powershell but below error message displayed. When i run the api in postman then it is working fine.

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402903: The specified value is not convertible to type ReleaseStartMetadata. Make sure it is convertible to type ReleaseStartMetadata and try again.","typeName":"Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000} At C:\Users\Raj.Negi\Desktop\PowerShell\TriggerVSTSrelease.ps1:35 char:11 + $result = Invoke-RestMethod -Uri $uri -Method POST -Body $params -Hea ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand Unable to locate Release Definition Id 860 At C:\Users\Raj.Negi\Desktop\PowerShell\TriggerVSTSrelease.ps1:40 char:6 + throw "Unable to locate Release Definition Id $($definitionId)" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (Unable to locat...finition Id 860:String) [], RuntimeException + FullyQualifiedErrorId : Unable to locate Release Definition Id 860

Powershell Code :

Param(
   [string]$vstsAccount = "demo",
   [string]$projectName = "Enterprise",
   [string]$definitionId = "860",
   [string]$keepForever = "true",
   [string]$personalAccessToken  = "asdfasdf"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) }

# Construct the REST URL
$uri = "https://$vstsAccount.vsrm.visualstudio.com/$projectName/_apis/release/releases?api-version=5.0-preview.3"

Write-Host "Uri :" $uri

$params = 
'[
{
    "definitionId": 860,
    "description": "Create Release from postman.",
    "artifacts": [],
    "isDraft": false,
    "reason": "Demo purpose",
    "manualEnvironments": null,
    "environmentsMetadata": null, 
    "properties": null, 
    "variables": null
}
]'

Write-Host " Json Body :" $params

# Invoke the REST call and capture the results
$result = Invoke-RestMethod -Uri $uri -Method POST -Body $params -Headers $headers -ContentType "application/json" -Verbose -Debug

# This call should only provide a single result; Capture the Build ID from the result
if ($result.count -eq 0)
{
     throw "Unable to locate Release Definition Id $($definitionId)"
}
else
{
    Write-host "Success!!!"
}

Postman Request :

{
    "definitionId": 860,
    "description": "Create Release from postman.",
    "artifacts": [],
    "isDraft": false,
    "reason": "Demo purpose",
    "manualEnvironments": null
}
rAJ
  • 1,295
  • 5
  • 31
  • 66
  • Can you also include the request you're sending with Postman? – Josh Gust Feb 18 '19 at 18:41
  • Added postman request. – rAJ Feb 18 '19 at 18:45
  • Without searching all over the internet, why are you wrapping your body in `[ ]` as well as `{ }`? – Josh Gust Feb 18 '19 at 19:01
  • Does using a [here-string](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6#here-strings) and updating the api version like @Shayki mentioned give you the desired results? Also, see [this question](https://stackoverflow.com/questions/41216277/tfs-api-create-release-with-powershell) – Josh Gust Feb 18 '19 at 23:18

3 Answers3

1

You'll notice that the body defined in documentation is the same as ReleaseStartMetadata. Try to specify the missing properties in your powershell $params variable

$params = '[ { "definitionId": 860, "description": "Trigger release from powershell.", "artifacts": [], "isDraft": false, "reason": "Demo purpose", "manualEnvironments": null, "environmentsMetadata": null, "properties": null, "variables": null } ]'

Body

enter image description here

ReleaseStartMetadata

enter image description here

Josh Gust
  • 4,102
  • 25
  • 41
1

I succeed to trigger release with minor changes:

1) The beginning of the URL is different and the preview is 8

$uri = "https://vsrm.dev.azure.com/$vstsAccount/$projectName/_apis/release/releases?api-version=5.0-preview.8"

2) The JSON body is in this format:

$params = 
@"
{
    "definitionId": 860,
    "description": "Create Release from PowerShell",
    "artifacts": [],
    "isDraft": false,
    "reason": "Demo purpose",
    "manualEnvironments": null,
    "environmentsMetadata": null, 
    "properties": null, 
    "variables": null
}
"@
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
0

Real solution is setting the Depth parameter of ConvertTo-Json cmdlet so that you use the full representation of the object in JSON. Source: https://blogs.msdn.microsoft.com/aseemb/2017/04/06/vs402903-the-specified-value-is-not-convertible-to-type-releasedefinition-make-sure-it-is-convertible-to-type-releasedefinition-and-try-again/

JohnTortugo
  • 6,356
  • 7
  • 36
  • 69