'Error while updating VSTS release definition from powershell

I use the APIs listed in the VSTS API documentation here. On modifying a variable and saving the definition the error I get from the server is VS402982: Retention policy is not set for the environment 'environmentName'.

The portion of the PS script that performs the update is -

$c = Invoke-WebRequest 'https://accountname.vsrm.visualstudio.com/projectname/_apis/release/definitions/definitionId' -Method Get -Headers @{Authorization = 'Bearer ' + $authtoken} 
$jsonObj = $c | ConvertFrom-Json
$url3 = "https://accountname.vsrm.visualstudio.com/projectname/_apis/release/definitions/definitionId?api-version=4.1-preview.3";

$contentType3 = "application/json"      
$headers3 = @{
    Authorization = 'Bearer ' + $authtoken
};

$d = $jsonObj | ConvertTo-Json;
Invoke-RestMethod -Method PUT -Uri $url3 -ContentType $contentType3 -Headers $headers3 -Body $d;

What could be wrong here?



Solution 1:[1]

Additionally to divyanshm's solution make sure encoding is correct:

$d = [Text.Encoding]::UTF8.GetBytes($d)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven