Updating project variables from the command line

I am working on generating a number of variables during the compile process and getting those into octopus so that we can better automate the rollback processes for some of our applications. I wrote a script where I was trying to upgrade an already existing variable valled RollbackProjectID with a hard coded value in the script. I get a remote server 500 error each time I try this. Is there something wrong with the statement I am using or do I need to update the project as a whole? Here is a copy of my script

$systemVariables = Invoke-RestMethod -Method get -Uri "http://server/api/variables/variableset-Projects-87/" -Headers @{"X-Octopus-ApiKey" = 'key'}

foreach($var in $systemVariables.Variables)
{
    if($var.Name = "RollbackProjectID")
    {
        $var.Value = "123445667"
        Invoke-RestMethod -Method put -Uri "http://server/api/variables/variableset-Projects-87" -Body ($systemVariables.Variables | ConvertTo-Json -Depth 5)-Headers @{"X-Octopus-ApiKey" = 'API-key'}
    }
}

Hi,

Thanks for getting in touch. I was able to get your script working as per the following.

$systemVariables = Invoke-RestMethod -Method get -Uri "http://server/api/variables/variableset-Projects-21" -Headers @{"X-Octopus-ApiKey" = 'API-KEY'}

foreach($var in $systemVariables.Variables)
{
    if($var.Name -eq "VariableName")
    {
        $var.Value = "123445667"
        Invoke-RestMethod -Method put -Uri "http://server/api/variables/variableset-Projects-21" -Body ($systemVariables | ConvertTo-Json -Depth 5)-Headers @{"X-Octopus-ApiKey" = 'API-KEY'}
    }
}

We have a Github repository with a ton of great examples for working with the Octopus API. You can find it at the following URL.

This is a great example similar to your example.

Hope this helps!

Rob

Rob,

thanks for that. I found another issue in the script above where the depth parameter on the convert to json will cause issues depending on how many variables there are. Thank you for your help.

Notice:

This issue has been closed due to inactivity. If you encounter the same or a similar issue and require help, please open a new discussion (if we asked for logs or extra details in this thread, consider including them in the new thread). If you are the creator of this thread and believe it should not be closed let us know via our support email.