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.""

1

There are 1 best solutions below

0
wade zhou - MSFT On

As per the error VS402865: An empty body in the POST request is not allowed., it's recommended to output the $requestBody content for a check.

I tried your script and fixed some syntax, it works on my side. I used system.accesstoken as the bearer token and add powershell task in DevOps release:

  1. Enable the option on agent job level:

enter image description here

  1. The PowerShell script:
$url = "https://vsrm.dev.azure.com/$(orgname)/$(projectname)/_apis/release/releases/$(Release.ReleaseId)?api-version=5.0"


$secretValue = "false"
$override = "false"
$variableValue = "var3value4"
$variableName = "var3"


$releaseDefinition = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"="Bearer    $env:SYSTEM_ACCESSTOKEN"; "Content-Type" = "application/json; charset=utf-8" }

$newVariable = New-Object PSObject
$newVariable | Add-Member Noteproperty allowOverride $override 
$newVariable | Add-Member Noteproperty isSecret $secretValue
$newVariable | Add-Member Noteproperty value $variableValue

$serializedVariable = $newVariable | ConvertTo-Json -Depth 3
$deserializedVariable = $serializedVariable | ConvertFrom-Json

$releaseDefinition.variables | Add-Member -Name $variableName -Value $deserializedVariable -MemberType NoteProperty -Force

$requestBody = ConvertTo-Json $releaseDefinition -Depth 100

echo $requestBody

#Error throw here when we add new release variable
$response = Invoke-RestMethod -Uri $url -Method Put -Headers @{"Authorization"="Bearer     $env:SYSTEM_ACCESSTOKEN"; "Content-Type" = "application/json; charset=utf-8" } -Body $requestBody

Release task succeeds:

enter image description here

Variable added:

enter image description here