Setting a variable on a tenant using PowerShell

Hi, I am trying to set a library variable for the tenant. I can create the tenant and can see the library variables but I am having trouble setting the value of a library variable for this tenant. I can see them, I can see set values but can’t understand how to set it via PowerShell.

$OctopusURI =
$apikey =
$Instance =

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI,$apikey
$repo = new-object Octopus.Client.OctopusRepository $endpoint
$TenantEditor = $repo.Tenants.CreateOrModify($Instance)
$Vars = $TenantEditor.Variables.Instance.LibraryVariables
$Vars.Values
$Vars.‘LibraryVariableSets-181’.Templates
$TenantEditor.Variables.Instance

Any help in the right direction would be greatly appreciated

Hi Matthew

Thanks for contacting us. To set the variable properly on the tenant you also need to access the variables by the id of the template used in library variable. Here’s some powershell for example.

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI,$apikey 
$repo = new-object Octopus.Client.OctopusRepository $endpoint 
$TenantEditor = $repo.Tenants.CreateOrModify($Instance)
$Vars = $TenantEditor.Variables.Instance.LibraryVariables
$VarSet = $Vars.'LibraryVariableSets-1'
$VarTemplate = $VarSet.Templates | Where-Object -Property Name -eq "VariableSetName"
$VarSet.Variables[$varTemplate.Id] = "NewVarValue"
$TenantEditor.Save()

Hope this helps.
Cam

Perfect, exactly what I wanted to do. Thanks, appreciate the rapid
response.