Hi Jeremy,
I don’t have access to Octopus server so can’t see the logs. Now I am getting “The remote server returned an error: (400) Bad Request”. The same updated json I am using in postman with same api key and working fine. I am using Octopus deploy version 2021.3 (Build 12372). Below is the code which I am using.
$ErrorActionPreference = “Stop”;
$octopusURL = “https://”
$octopusAPIKey = “API-”
$header = @{ “X-Octopus-ApiKey” = $octopusAPIKey }
$spaceName = “Default” # Name of the Space
$tenantName = “GEN1” # The tenant name
$variableTemplateName = “JobFrequency” # Choose the template Name
$newValue = “1” # Choose a new variable value, assumes same per environment
$NewValueIsBoundToOctopusVariable = $False # Choose $True if the $newValue is an Octopus variable e.g. #{SomeValue}
$space = (Invoke-RestMethod -Method Get -Uri “$octopusURL/api/spaces/all” -Headers $header) | Where-Object { $_.Name -eq $spaceName }
$tenantsSearch = (Invoke-RestMethod -Method Get -Uri “$octopusURL/api/$($space.Id)/tenants?name=$tenantName” -Headers $header)
$tenant = $tenantsSearch.Items | Select-Object -First 1
$variables = (Invoke-RestMethod -Method Get -Uri “$octopusURL/api/$($space.Id)/tenants/$($tenant.Id)/variables” -Headers $header)
$projects = $variables.ProjectVariables | Get-Member | Where-Object { $_.MemberType -eq “NoteProperty” } | Select-Object -ExpandProperty “Name”
foreach ($projectKey in $projects) {
if (“Projects-123” -eq $projectKey) {
# Get connected project
$project = $variables.ProjectVariables.$projectKey
$projectName = $project.ProjectName
Write-Host “Working on Project: $projectName ($projectKey)”
$variableTemplate = ($project.Templates | Where-Object Name -eq $variableTemplateName | Select-Object -First 1)
if ($null -ne $variableTemplate) {
$variableTemplateId = $variableTemplate.Id
$variableTemplateIsSensitiveControlType = $variableTemplate.DisplaySettings["Octopus.ControlType"] -eq "Sensitive"
Write-Host "Found templateId for Template: $variableTemplateName = $variableTemplateId"
$projectConnectedEnvironments = $project.Variables | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | Select-Object -ExpandProperty "Name"
# Loop through each of the connected environments variables
foreach ($envKey in $projectConnectedEnvironments) {
if ($envKey -eq "Environments-16") {
# Check for Environment project template entry, and add if not present
if ($null -eq $project.Variables.$envKey.$variableTemplateId) {
$project.Variables.$envKey | Add-Member -MemberType NoteProperty -Name $variableTemplateId -Value $newValue
}
# Check sensitive control types differently
if ($variableTemplateIsSensitiveControlType -eq $True) {
# If $newValue denotes an octopus variable e.g. #{SomeVar}, treat it as if it were text
if ($NewValueIsBoundToOctopusVariable -eq $True) {
Write-Host "Adding in new text value (treating as octopus variable) in Environment '$envKey' for $variableTemplateName"
$project.Variables.$envKey.$variableTemplateId = $newValue
}
else {
$newSensitiveValue = [PsCustomObject]@{
HasValue = $True
NewValue = $newValue
}
Write-Host "Adding in new sensitive value = '********' in Environment '$envKey' for $variableTemplateName"
$project.Variables.$envKey.$variableTemplateId = $newSensitiveValue
}
}
else {
Write-Host "Adding in new value = $newValue in Environment '$envKey' for $variableTemplateName"
$project.Variables.$envKey.$variableTemplateId = $newValue
}
}
}
}
else {
Write-Host "Couldnt find project template: $variableTemplateName for project $projectName"
}
}
}
$variablesOld = (Invoke-RestMethod -Method Get -Uri “$octopusURL/api/$($space.Id)/tenants/$($tenant.Id)/variables” -Headers $header)
$vold = (($variablesOld | convertto-json -Depth 10) -split ‘\r?\n’)
$vnew = (($variables | convertto-json -Depth 10) -split ‘\r?\n’)
Write-Host “-- – – – Below is json comparison details”
(Compare-Object -ReferenceObject $vold -DifferenceObject $vnew) | Format-Table -Wrap
Invoke-RestMethod -Method Put -Uri “$octopusURL/api/$($space.Id)/tenants/$($tenant.Id)/variables” -Headers $header -Body ($variables | convertto-json -Depth 10) -ContentType application/json