Enumerate project level variables in PowerShell

When writing a step as a PowerShell script, I have access to variables through the $OctopusParameters dictionary. This dictionary contains all variables declared on the project along with variables declared in any included variable set and a bunch of system variables.

Is there any way to a dictionary with only the variables declared directly on the project? Use case: I need to build a JSON file containing all application configuration as part of the deployment process.

Hi,

Thanks for reaching out! There’s not direct way from the deployment to only get the project variables, but you can get them using the Octopus API. I’m gonna show you 2 methods to do it:

A) Do a GET to the project variable set url. The URL format looks like this

http://[YourOctopusURL]/api/variables/variableset-projects-801

Projects-801 is the project id, which you can get during the deployment from the variable $OctopusParameters['Octopus.Project.Id']

This will return a JSON like this one, which you can then manipulate using powershell.

B) Use the cmdlet Get-OctopusVariableSet from the open source project Octoposh. The command will be like this

$VariableSet = Get-Get-OctopusVariableSet -Projectname [YourProjectName]

Then you can use $VariableSet.Variables to create your json

The advantage of this method over (A) is that this one returns the actual names of the resources listed on the scopes like Production and Computer1 instead of Environments-1 and machines-2 which wont be that helpful on your JSON

The disadvantage is that you’re gonna have to install the module on the machine that will be using the cmdlet.

Please keep in mind that this is an open source project, so all questions about it must be done on project’s site

Hope that helps!

Dalmiro