Powershell script is setting new release variable but it started failing suddenly
$url = "https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/$releaseId?api-version=5.0"
$releaseDefinition = Invoke-RestMethod -Uri $url -Method Get -Headers @{ "Authorization"="Bearer $authenticationToken" "Content-Type" = "application/json; charset=utf-8" }
$newVariable = New-Object PSObject
$newVariable | Add-Member Noteproperty allowOverride $False
$newVariable | Add-Member Noteproperty isSecret $secretValue
$newVariable | Add-Member Noteproperty value $variableValue
$serializedVariable = $newVariable | ConvertTo-Json -Depth 3
$deserializedVariable = $serializedVariable | ConvertFrom-Json
** This is problematic line.If i comment out this line then request doesn't fail.When I log the releaseDefinition.variables and i see new variable is added.**
$releaseDefinition.variables | Add-Member -Name $variableName -Value $deserializedVariable - MemberType NoteProperty -Force
$requestBody = ConvertTo-Json $releaseDefinition -Depth 100
#Error throw here when we add new release variable
$response = Invoke-RestMethod -Uri $url -Method Put -Headers @{ "Authorization"="Bearer $authenticationToken" "Content-Type" = "application/json; charset=utf-8" } -Body $requestBody
Error message: "Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402865: An empty body in the POST request is not allowed.""
As per the error
VS402865: An empty body in the POST request is not allowed., it's recommended to output the$requestBodycontent for a check.I tried your script and fixed some syntax, it works on my side. I used
system.accesstokenas thebearer tokenand add powershell task in DevOps release:Release task succeeds:
Variable added: