Update tenant variables with powershell


I’m trying to update a common variable for all of our tenants with powershell but is getting a responsecode 500. Can you see what is the issue with the following script?

$allTenants = Invoke-WebRequest -uri "https://[instance].octopus.com/api/tenants/all?apikey=API-KEY" | ConvertFrom-Json

foreach($tenant in $allTenants)
    $tenantVariables = Invoke-WebRequest -uri "https://[instance].octopus.com/api/tenants/$($tenant.Id)/variables?apikey=API-KEY" | ConvertFrom-Json
    $tenantVariables.LibraryVariables.'LibraryVariableSets-2'.Variables | Add-Member -NotePropertyName "a9599425-68bb-4978-98b6-df8e39f162c6" -NotePropertyValue $tenant.Name
    $jsonTenant = ConvertTo-Json $tenantVariables

    Invoke-WebRequest -Uri "https://[instance].octopus.com/api/tenants/$($tenant.Id)/variables?apikey=API-KEY" -Method Put-Body $jsonTenant


Hi Nils,

Thanks for getting in touch! I managed to get tenant variables and persist them without making any changes by specifying -Depth 10 in the ConvertTo-Json command. Unfortunately, the default value for it is 2 and the server gets a malformed request.

Problems like this one can be avoided by using our C# client from PowerShell. You can find a few simple examples here: https://github.com/OctopusDeploy/OctopusDeploy-Api/tree/master/Octopus.Client/PowerShell/Tenants.

Please, let me know how you go.




That helped. Thanks.


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