Octopus API failure, while trying to replace/create variables value in library variable set

Hi Team,

I have a requirement where I need to update octopus variable set values.
Am using below script to replace the values of the variables.

##########################################################
$systemVariables = Invoke-RestMethod -Method get -Uri “$OctoURL/api/Spaces-1/variables/variableset-LibraryVariableSets-466” -Headers @{“X-Octopus-ApiKey” = $ApiKey}

function ReplacetheValue{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
$Variabletoreplace,

    [Parameter(Mandatory=$true)]
    $NewValue     
)

Invoke-RestMethod -Method put -Uri “$OctoURL/api/Spaces-1/variables/variableset-LibraryVariableSets-466” -Body ($systemVariables | ConvertTo-Json -Depth 5)-Headers @{“X-Octopus-ApiKey” = $ApiKey}
}

ReplacetheValue -Variabletoreplace “$EachComponent.$Env_Name.$Lab_Name” -NewValue $version
##################################################
I am passing the values to both the parameters.

It works most of the time, but recently we have been getting lot of issue/ errors as below:
“System.Net.WebException: The remote server returned an error: (400) Bad Request.”

Where after getting this error, I have modified the script to retry couple of time, but it is of no use
for each try I am getting the same error again and again. & value in not replaced with new value in octopus variable set.

Can you please help me out to resolve this issue, as it is causing a lot of failures and leading to inconsistent data being available in library variable set.

Hi,

Thanks for reaching out.

I’ve actually written a pretty lengthy script for adding/modifying variables in a project.

You could change the function Function Get-OctopusProjectVariables to instead get a library variable set rather than a project variable set. https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Variables/AddOrEditVariablesWithScoping.ps1

Can you please give that a try and see if it works for your use case?

Please let me know.

Thanks,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.