Variable Preview API - Client - Example please!

Hi -

I’m trying to generate a project variable preview using the client in PowerShell. The purpose is (hopefully) to export the preview so it can be reviewed by the project owners outside of the UI. I’ve been playing with the ‘VariableSets.GetVariablePreview’ method with no success. Just keep bumping into a ‘Cannot find an overload for “GetVariablePreview” and the argument count’ error.

Code Example:
$ep = New-Object Octopus.Client.OctopusServerEndpoint ‘http://octpousurl.com’,‘MyAPIKey’
$repo = New-Object Octopus.Client.OctopusRepository $ep
$prjObj = $repo.Projects.FindByName(‘MyProj’)
$repo.VariableSets.GetVariablePreview($prjObj)

It would be helpful if you could provide an example of how to do this via the client.

Thanks!

Hi @snaulls,

Thanks for reaching out. I did some digging and I think the GetVariablePreview needs different information than the project object. The following code worked for me (I dug down into the spaces first, not sure if you need to do that on your end.)

Add-Type -Path 'C:\temp\Octopus.Client.dll' 
$OctopusUrl = ""
$ApiKey = ""
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$ApiKey
$client = new-object Octopus.Client.OctopusClient $endpoint

$spaceName = "Default"
$space = $client.ForSystem().Spaces.FindByName($spaceName)
$spaceRepository = $client.ForSpace($space)

$vars = New-Object Octopus.Client.Model.VariableSetResource
$vars = $spaceRepository.VariableSets.GetVariablePreview("Projects-81","Channels-81",$null,$null,$null,"Environments-1",$null,$null)

The GetVariablePreview function is matching on 8 different parameters as shown here:

public VariableSetResource GetVariablePreview(string project, string channel, string tenant, string runbook, string action, string environment, string machine, string role)

I found this information in this code:https://github.com/OctopusDeploy/OctopusClients/blob/4062372580d70c0da609b82c39fb1da14422b38c/source/Octopus.Client/Repositories/VariableSetRepository.cs

Please let me know if that works for you.

Thanks,
Jeremy

Awesome! Lifesaver! Appreciated!

1 Like

Hi @snaulls,

I’m glad to hear it worked for you. Thanks for updating me.

Have a great rest of your week!

Best,
Jeremy

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