Common Template Variables not saving


I am attempting to programmatically set Common Variables on a Tenant, and for some reason they will not save when committed back to the server.

Here is the snippet of code that I think should work, but any guidance would be appreciated:

$tenantVars = $repository.Tenants.GetVariables($tenant)
$projectVars = $tenantVars.ProjectVariables[$projectid]
$commonVarSet = $repository.LibraryVariableSets.FindByName("Enterprise")
$commonVars = $tenantVars.LibraryVariables[$commonVarSet.Id]

$commonVal = @{
    "LogoUrl" = ""
    "Ticker" = $ticker

$tenantEditor = $repository.Tenants.CreateOrModify($tenant.Name)

$commonVal.Keys | Foreach-Object {
    $name = $_
    $varId = ($commonVars.Templates | Where-Object { $_.Name -eq $name }).Id
    $varVal = $commonVars.Variables[$varId].Value

    if($varVal -ne $commonVal[$_]) {
        $commonVars.Variables[$varId] = $commonVal[$_]

Hi @jturcic ,

Thanks for reaching out.

I’m digging into this on my end to see what the best path forward is to save the common variables object to the repository.

I’ll update you as this progresses.

Please feel free to reach out in the meantime.


Hi Jeremy,

I think I figured this first part out. I changed the $commonVars variable as follows:

$commonVars = $tenantEditor.Variables.Instance.LibraryVariables[$commonVarSet.Id]

I’m still working through a similar problem with some of the ProjectVariables, which may be the same issue, but I’ll report back on what I figure out (if anything).

Thanks for looking into this.

Hey Jamey,

Thanks for the update.

Have you taken a look at the syntax here?

Please let me know if the example script helps you get unstuck there.

Also thank you for the update on the common variables fix. I was working on it on my end and trying a ton of ways to get it to save and having issues as well. I mostly work with our data via REST so Octopus.Client syntax can elude me at times.


Hi Jeremy,

Yes, our script is loosely based on that example. Just some syntactical difference, but accomplishes the same thing.

I figured out my issue. We have a mix of project variables that are conditionally needed/used. In some cases they just have a default value, and others, when they aren’t needed, we fill them in with a null value.

The problem I was having is that early in the script we load up all of the variables, and set their values. If the value isn’t needed, we set it to a null value, however later in the script we loop and add the default value to the variable set before writing it back to the server. In our case, when a variable doesn’t have a default value it was removing the null string and setting it to blank causing us to have empty vars on the server. The fix was to check if a default value had a length of 0, and not modify/remove the null string. :slight_smile:

Thanks again for your help.

Hi Jamey,

You’re very welcome. Thanks so much for updating the thread with the resolution.

I hope you have a great rest of your week and please reach out in the future with any other issues.


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