Add a single variable to a project not working

I want to add a single variable to the project. Not part of a variable set but just one single variable. I am getting an error saying:
This is my code and does not give me an error but it does not add the variable to the project.
Any ideas why??

#Add a new variable to the project

Add-Type -Path 'C:\temp\Octopus.Client.dll'

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$APIKey 
$client = New-Object Octopus.Client.OctopusClient $endpoint

$space = $client.ForSystem().Spaces.FindByName($SpaceName) 
$spaceRepository = $client.ForSpace($space)

#Add a new variable to the project
$project = $spaceRepository.Projects.FindByName($ProjectName)
$variableset = $spaceRepository.VariableSets.Get($Project.links.variables)
$newVariable = New-Object Octopus.Client.Model.VariableResource
$newVariable.Name = "PackageName"
$newVariable.Value = "Package.Common.DataConcerns"
$variableset.Variables.Add($newVariable)

Hi @mikepower79,

Thanks for getting in touch!

Your script is almost perfect, it just needs one final line to push the changes made to the variable set.
e.g.

$variableset.Variables.Add($newVariable)
$spaceRepository.VariableSets.Modify($variableset)

Regards,
Paul

Hi Paul,
Thats great thanks alot.

Mike

1 Like

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