1
votes

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
}
3
Can you also include the request you're sending with Postman?Josh Gust
Added postman request.rAJ
Without searching all over the internet, why are you wrapping your body in [ ] as well as { }?Josh Gust
Does using a here-string and updating the api version like @Shayki mentioned give you the desired results? Also, see this questionJosh Gust

3 Answers

1
votes

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
}
"@
1
votes

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

0
votes