Creating a project and unable to add Library Variable Set in Octopus Deploy

I am creating a new project in octopus using powershell script and I am having an issue with adding a Library Variable Set.
I find the LibraryVariableSet below and I add it to the $Project and then save it.
But the LibraryVariableSet is not added to my project. It does not throw an error the script runs fine and the $LibraryVariableSet is returning the correct Library Variable Set. Cant figure out why the Library Variable Set is not adding to the project. Any ideas why this may be happening??

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$client = new-object Octopus.Client.OctopusClient $endpoint

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

$projectName = "My Project"
$lifecycleName = "Default Lifecycle"
$projectGroupName = "Default Project Group"
$VariableSet = "nuget-variableset"

$projectGroup = $spaceRepository.ProjectGroups.FindByName($projectGroupName)
$lifecycle = $spaceRepository.Lifecycles.FindByName($lifecycleName)
$LibraryVariableSet = $spaceRepository.LibraryVariableSets.FindByName($VariableSet)

$project = $spaceRepository.Projects.CreateOrModify($ProjectName, $ProjectGroup, $Lifecycle, 

Hi @mikepower79,

Thanks for getting in touch!

Sorry to see you are having issues getting this working.

We have example scripts of how to create a project and add a library variable set to projects in our GitHub repo, and I’ve attached them below:

If you have any issues moving forward, please do get in touch.

Kind regards,

Hi Ziaul,
That worked great. I want to add a single variable to the project. Not part of the variable set but just one single variable. I am getting an error saying:
“You cannot call a method on a null-valued expression.”

Would this work:

#Add a new variable to the project
$newVariable = new-object Octopus.Client.Model.VariableResource
$newVariable.Name = "PackageName"
$newVariable.Value = "Package.Common.DataConcerns"

# Find the project you want and add the variable to
foreach ($name in $ProjectName) {
    $project = $spaceRepository.Projects.FindByName($name)


Hi Mike,

Apologies in the delay getting back to you.

the majority of the code is there, I would adjust it a little and you will be good to go:

    $Varname = "name"
    $varvalue = "value"

    $space = $client.ForSystem().Spaces.FindByName($SpaceName) 
    $spaceRepository = $client.ForSpace($space)
    $newVariable = New-Object Octopus.Client.Model.VariableResource
    $project = $spaceRepository.Projects.FindByName($ProjectName)
    $variableset = $spaceRepository.VariableSets.Get($Project.links.variables)
    $newVariable.Name = $Varname
    $newVariable.Value = $varvalue

I hope this helps.

Kind regards,

Hi Ziaul,
Thats perfect thanks alot for your help.


1 Like

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