Filter Variables based on scope

Hi,

I am looking to script the creation of variables based on roles / tenant details…

Hopefully this will make sense

my roles that will require a variable setting … (but only if there isnt a matching variable already)
$roles = @("APP,“WEB”) etc etc

I get all the tenants that belong to the project i am working on

$alltenants = $repository.Tenants.FindAll() | ?{$_.ProjectEnvironments.ContainsKey(“Projects-85”)}

Working with the tenant flags is where i am getting stuck a little … and here is where i’m at … I have created this variable through the UI

Id : 6f50f884-ebc1-0700-01f0-aae66e38ea99
Name : App
Value :
Type : Sensitive
Scope : {[Environment, Environments-64], [TenantTag, Client/CF, Unique Identifier/SIT1]}
IsEditable : True
Prompt :
IsSensitive : True

When i check the tenant

    $tenant.ProjectEnvironments
    $tenant.TenantTags

I get these values returned

Key : Projects-85
Value : {Environments-64}

I would only need to check the bold items below

Hosting/Azure Shared
Client/CF
Unique Identifier/SIT1
Version/14

Based on what i would need to check there is a matching variable for this tenant and role …

how can i filter returned variables based on the environments or tentant tags … filtering by name is easy i can find all the app variables … but finding variables based on EV/tags is a little tricky …

$variablesetname = “Account Passwords”

$variableset = $repository.LibraryVariableSets.FindByName($variablesetname)
$variableset = $repository.VariableSets.Get($variableset.VariableSetId)
$appvariables = $variableset.Variables | ?{$_.name -match “APP”}

Id : 6f50f884-ebc1-0700-01f0-aae66e38ea99
Name : App
Value :
Type : Sensitive
Scope : {[Environment, Environments-64], [TenantTag, Client/CF, Unique Identifier/SIT1]}
IsEditable : True
Prompt :
IsSensitive : True

Id : d3037077-a535-b364-5fde-d70aa3b20a22
Name : App
Value : testing123
Type : String
Scope : {[Environment, Environments-1], [TenantTag, Client/Vanilla, Unique Identifier/Team1]}
IsEditable : True
Prompt :
IsSensitive : False

so from the tenant details above how can i filter the 2 results down to one based on EV 64

and then tenant tags? as there will be more per EV

I would like to do something similar to

$appvariables = $variableset.Variables | ?{$_.Scope.Environment -contains “Environments-64”} (though that obvs doesn’t work)

let me know if you need any more info on this

Thanks

Well… without throwing any curve balls into the mix i managed to script something that’s returning what i need

$roles = @(“APP”,“IWEB”,“PVTWEB”,“PUBWEB”,“SRVCS”,“SSR”,“SSI”)
$variablesetname = “Account Passwords”

Add-Type -Path .\Newtonsoft.Json.dll
Add-Type -Path .\Octopus.Client.dll

$headers = @{“X-Octopus-ApiKey”="$MyAPIKey";}

$endpoint = new-object Octopus.Client.OctopusServerEndpoint ($OctopusURI, $MyAPIKey)
$repository = new-object Octopus.Client.OctopusRepository $endpoint
$variablesets = $repository.LibraryVariableSets.Findall()
$Environments = $repository.Environments.FindAll()

$link = ((($variablesets | ?{$_.name -eq “$variablesetname”}).links).values | Select-Object -last 1)

$t = Invoke-restmethod ("$OctopusURI/$link") -Headers $Headers -Method Get

$alltenants = $repository.Tenants.FindAll()
$array = @()
foreach($tenant in $alltenants)
{
write-host $tenant.Name -ForegroundColor Blue
$tenanttags = $tenant.TenantTags
$tenantenvironments = @($tenant.ProjectEnvironments.Values) | select-object -Unique
foreach($uniqueev in $tenantenvironments)
{
write-host $uniqueev -ForegroundColor Cyan
foreach($role in $roles)
{
write-host $role -ForegroundColor Yellow
$newvairables = $T.Variables | ?{$_.name -eq $role}
$matched = $false
foreach($v in $newvairables)
{
$count = 0
if($v.scope.environment -eq $uniqueev)
{
foreach($tag in $tenanttags.GetEnumerator())
{
foreach($item in $v.Scope.TenantTag)
{
if($tag -match “^client|^Unique Identifier”)
{
if($tag -match $item)
{
$count ++
}
}
}
}
}
if($count -eq 2)
{
$matched = $true
write-host “$role vairable exists” -ForegroundColor Green
echo “tenanttags $tenanttags”
$v.Scope.Environment
$v.Scope.TenantTag
echo “”
break
}
else
{
$matched = $false
# $role
# write-host “vairable doesnt exist” -ForegroundColor Yellow
# echo “tenanttags $tenanttags”
# echo “ev = $uniqueev”
# $v.Scope.Environment
# $v.Scope.TenantTag
# echo “”
}
}
if($matched -eq $false)
{
write-host “Going to create $role now” -ForegroundColor Red
}
}

        }
}

Hi Kris,

Thanks for getting in touch,

I’m glad to hear you were able to find a solution to this, I greatly appreciate you letting us know the outcome!

If you require any assistance in the future, please don’t hesitate to reach out :slight_smile:

Have a great day!

Kind Regards,

Reece

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