How to fetch only Project Template variables and its value from project with respect to Environment and Tenant

Hi Team,

I requirement to fetch only Project Template variables and its value from project with respect to Environment and Tenant.

Thanks
Santhosh Shet

Hi Santhosh,

Thanks for the question!

Please could you provide me with a little more information? Would you like to retrieve variables using the API? If so, this example may help: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/cfd2cbd642e542994814a065ab1d3dd9a779d8a0/REST/PowerShell/Variables/GetTenantVariablesPerProject.ps1

If you have any further questions please get in touch!

Thanks!
Lianne.

Hi Lianne,

Thanks for your reply , i have requirement to get only Project Template variable (Highlighted in screenshot below) with respect to connected tenant and environment.

we will get all the variable by using the script you have given.

I hope you have solution for getting only Project Template variable .

Hi @santoshuponi,

Thanks for coming back to us.

We have a script you can use to get the Project Variables per environment which would likely help you get this information. You will be able to use this script here as the start of your script to get the information you’re after.

Thanks,

Derek

Hi Derek,

I am able to get the values for “Project Templates” from the API below, by filtering Tenant , Project , Environment .
https://deploy.eurofins.local/Octopus/api/Spaces-1/projectgroups/ProjectGroups-XX/projects?skip=0&take=2147483647

i am getting value for the case below, please find the screenshot 1.1 (I.e just variable is created in Project Templates and no default value assigned )
you can see the result in screenshot 1.2.(You could notice Variable name , Ennvironment-Tenant name and value returned )
**Screenshot **
1.1 image
1.2 image

Challenge here is , i am getting the empty value if in below case, please find the screenshot 2.1 and 2.2 (i.e in some default values assigned in Project),
you can see the result in screenshot 2.2(You could notice only variable Name and Ennvironment-Tenant name , No value returned )
**screenshot **
2.1


2.2 image

Please help me to get away from this challenge :slight_smile:

Thanks,
Santhosh Shet

Hi Santosh,

Thanks for coming back to us.

Are you requesting the content of this field in your script? You can see examples on how to get Content using the content property on https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.

If you have a look at your first example i. 1.1 and 1.2, it’s printing out the variable, and the value which you have specified, but in the second sample, it’s only printing out the variable and not the value specified.

Thanks,

Derek

Hi Derek,

"If you have a look at your first example i. 1.1 and 1.2, it’s printing out the variable, and the value which you have specified, but in the second sample, it’s only printing out the variable and not the value specified.
"-Yes
I need to get both variable and value in second example.

Please find the script below,

$OctopusAPIkey = #Octopus API Key
$OctopusURL = #Octopus URL

$project = #Project ID
$tenant = #Tenant ID
$ProjectGroup = #ProjectGroup ID
$projectList = @()
$templatesid = @()
$environment = @()
$header = @{ “X-Octopus-ApiKey” = $octopusAPIKey }

$tenantVariables = (Invoke-WebRequest -UseBasicParsing “$OctopusURL/api/Spaces-1/tenants/$tenant/variables” -Headers $header).content | ConvertFrom-Json

$projectList = $tenantVariables.ProjectVariables

#need to collect all project
$Projects = (Invoke-WebRequest -UseBasicParsing “$OctopusURL/api/Spaces-1/projectgroups/$ProjectGroup/projects?skip=0&take=2147483647” -Headers $header).content | ConvertFrom-Json
$projectId = $Projects.Items.Id

#need to collect all environments
$Environmets = (Invoke-WebRequest -UseBasicParsing “$OctopusURL/api/environments/all?” -Headers $header).content | ConvertFrom-Json
$EnvironmetsId = $Environmets.Id

$All_Project_IDs = $projectId
$All_Project_IDs | ForEach-Object {

if ($projectList -match "$_")
      {
        $project_Id = "$_"
                   
        #all  TemplatesID
        $templatesid = $tenantVariables.ProjectVariables.$project_Id.Templates.Id
                  
        #environment
        $environment = $tenantVariables.ProjectVariables.$project_Id.Variables 

        #need to collect all environment
        $EnvironmetsId | ForEach-Object {
         if ($environment -match "$_")
            { 
              $environments_Id = "$_"
              $templatesid |  ForEach-Object {
              $ids= "$_"
              
              $name= $tenantVariables.ProjectVariables.$project_Id.Templates | Where-Object Id -eq $_
              $value = $tenantVariables.ProjectVariables.$project_Id.Variables.$environments_Id
              $templateName =$name.Name
              if($templateName -notmatch 'Password')
              { 
                 $tenantVariables.ProjectVariables.$project_Id.ProjectName
                 $tenantVariables.ProjectVariables.$project_Id.ProjectId
                 Write-Host "$templateName"
                 Write-Host "$environments_Id-$tenant"
                 $tenantVariables.ProjectVariables.$project_Id.Variables.$environments_Id.$_
              }
             
              }
            }          

        }
        
    }

}

Hi Santosh,

Thanks for seding this over.

Out of interest, what do you want to do with the details? Do you want to update it?

Thanks,

Derek

Hi Santosh,

I’ve tried this locally and I am getting the results back fine. We made some amendments to your script:

Octopus Url

$OctopusURL = “”

API Key

$OctopusAPIkey = “”
$project = “” #Project ID
$tenant = “” #Tenant ID
$ProjectGroup = “” #ProjectGroup ID
$projectList = @()
$templatesid = @()
$environment = @()
$header = @{ “X-Octopus-ApiKey” = $octopusAPIKey }
$tenantVariables = (Invoke-WebRequest -UseBasicParsing “$OctopusURL/api/Spaces-1/tenants/$tenant/variables” -Headers $header).content | ConvertFrom-Json
$projectList = $tenantVariables.ProjectVariables
#need to collect all project
$Projects = (Invoke-WebRequest -UseBasicParsing “$OctopusURL/api/Spaces-1/projectgroups/$ProjectGroup/projects?skip=0&take=2147483647” -Headers $header).content | ConvertFrom-Json
$projectId = $Projects.Items.Id
#need to collect all environments
$Environmets = (Invoke-WebRequest -UseBasicParsing “$OctopusURL/api/environments/all?” -Headers $header).content | ConvertFrom-Json
$EnvironmetsId = $Environmets.Id
$All_Project_IDs = $projectId
$All_Project_IDs | ForEach-Object {
if ($projectList -match “$") {
$project_Id = "$

#all TemplatesID
$templatesid = $tenantVariables.ProjectVariables.$project_Id.Templates.Id
#environment
$environment = $tenantVariables.ProjectVariables.$project_Id.Variables
#need to collect all environment
$EnvironmetsId | ForEach-Object {
if ($environment -match “$")
{
$environments_Id = "$

$templatesid | ForEach-Object {
$ids= "$"
$name= $tenantVariables.ProjectVariables.$project_Id.Templates | Where-Object Id -eq $

$value = $tenantVariables.ProjectVariables.$project_Id.Variables.$environments_Id
$templateName = $name.Name
if($templateName -notmatch ‘Password’)
{
$projectName = $tenantVariables.ProjectVariables.$project_Id.ProjectName
$projectId = $tenantVariables.ProjectVariables.$project_Id.ProjectId
Write-Host “$projectName ($projectId)”
Write-Host “TemplateName: $templateName”
Write-Host “Environment: $environments_Id-$tenant”
#$tenantVariables.ProjectVariables.$project_Id.Variables.$environments_Id
$projVariable = $tenantVariables.ProjectVariables.$project_Id.Variables.$environments_Id.$_
Write-Host “projVariable value is: $projVariable”
Write-Host
}
}
}
}
}
}

image

Let me know how you get on.

Thanks,

Hi Derek,

Thanks for your help :slight_smile:
i will try from my end now.

Regards
Santosh Shet

Hi Santosh,

Please let me know how you get on.

All the best,

Derek

Hi Derek,

Still i could get like below,
image

Project variable value is not coming.

Hi @santoshuponi,

What version are you running on? This is working on our most recent version.

Thanks,

Derek

Hi Derek,

We are using version v2020.1.17 , will this version works !

Thanks
Santhosh Shet

Hi Santosh,

This should work fine. Can you confirm that the API key you’re using has permission to read the variable?

Thanks

Derek

Hi derek,

Let me check with Admin API Key ,

Thanks
Santhosh Shet

Hi Derek,

No luck , its not working for me.

Thanks
Santhosh Shet

Hi Santosh,

Thanks for your patience.

I think we’ve uncovered a bug. I’ve spun up a test instance of 20120.1.17 and ran the script fresh. I got the below returned:

image

This is with the below set:

We uncovered a workaround that will get this returning for you. In Tenants, browse to the Variables page as below:

Clear the affected variable, and you will see a button appear to reset to default.

Then hit save. If you run the script again, you should see your variable appear.

image

We’ll raise this as a bug, but I would recommend upgrading Octopus when you can, and this shouldn’t happen.

Thanks,

Derek

Hi Derek,

Thanks for your support ,
Yes could see the variable value returned if i set to Default value, but in my case all tenant value might not be default and there are chances to get change (Other than Default).
It would be great if i get values of all project template variable associated w.r.t to tenants and environments.

Thanks
Santhosh Shet

Hi Santosh,

Thanks for coming back to me.

Could you try the fix mentioned above for the values that are not returning please? If you try saving the values that are not returning, then these should return. Our recommendation would be to upgrade to the latest version if you can’t get these values back.

Thanks,

Derek