LibraryVariableSets.Create

Hi,
I am using LibraryVariableSets.Create to create a new LibraryVariableSet per space.
I am passing in a name, description and a spaceId as I want to select the space I want the new LibraryVariableSet added to.

code snippet:
#Create a new Library Variable Set
$libraryVariableSet = New-Object Octopus.Client.Model.LibraryVariableSetResource
$libraryVariableSet.Name = $LibraryVariableSetName
$libraryVariableSet.SpaceId = $SpaceId
$libraryVariableSet.Description = $LibraryVariableSetDescription
$libraryVariableSet = $repository.LibraryVariableSets.Create($libraryVariableSet)

This is all good and when I get the output from $libraryVariableSet I get the following:
image

But when I move to the nest bit of code where I add variables to the LibraryVariableSet the $SpaceId reverts back to the default Spaces-1
#Add variable ArtifactoryLocalRepository with 2 values to the new Library Variable Set
$Variable1 = New-Object Octopus.Client.Model.VariableResource
$Variable1.Name = “ArtifactoryLocalRepository”
$Variable1.Value = $ArtifactoryLocalRepository_ArtifacoryPrereleaseValue
$variable1.Scope.Add([Octopus.Client.Model.ScopeField]::Environment, (New-Object
Octopus.Client.Model.ScopeValue(“Artifactory-Prerelease”)))
$variables = $repository.VariableSets.Get($libraryVariableSet.VariableSetId)
$variables.Variables.Add($variable1)
$repository.VariableSets.Modify($variables)
image

Any idea why this is happening??

Hey Mike,

Thanks for reaching out.

This does seem like an interesting one. Would you be able to attach your full script so I can take a look at it? If it needs to be private, feel free to direct message it to me.

Thanks,
Jeremy

Hi Jeremy,
I have added my script. thanks for taking a look.

OctopusAddNewVariableSet.ps1 (6.8 KB)

Hey Mike,

I did some reproduction and I think I found the problem.

I believe the problem is the code never digs into a space repository so it does its work in the default space. While your output shows “SpaceId : Spaces-167”, I believe if you went and checked that space in the GUI, it would actually reside in the default space. I ran your code as is just to create the variable set and nothing else as a test and it threw it in the default space even when I gave it the correct spaceId.

I altered the code to look like this:

#THIS CREATES THE ENDPOINT AND CLIENT (change repository to client, and the object it creates)
    $endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$ApiKey
    $client = new-object Octopus.Client.OctopusClient $endpoint

#THIS CODE ENTERS THE DESIRED SPACE (you will need to add all of this I believe)
    $spaceName = "Second Space"
    $space = $client.ForSystem().Spaces.FindByName($spaceName)
    $spaceRepository = $client.ForSpace($space)

#Create variableset within spaceRepository (I just hardcoded some stuff here for testing)
    $LibraryVariableSetName = "Test This"
    $SpaceId = "Spaces-2"
    $LibraryVariableSetDescription = "Test Description"
    $libraryVariableSet = New-Object Octopus.Client.Model.LibraryVariableSetResource
    $libraryVariableSet.Name = $LibraryVariableSetName
    $libraryVariableSet.SpaceId = $SpaceId
    $libraryVariableSet.Description = $LibraryVariableSetDescription
#you have to change this to hit spaceRepository instead of repository
    $libraryVariableSet = $spaceRepository.LibraryVariableSets.Create($libraryVariableSet)

#Create first variable (I also hard coded some stuff, and changed it to hit spaceRepository. Also, you need the scope to be the environment Id, not the environment name (at least in my testing)
    $Variable1 = New-Object Octopus.Client.Model.VariableResource
    $Variable1.Name = "ArtifactoryLocalRepository"
    $Variable1.Value = "Test Value"
    $variable1.Scope.Add([Octopus.Client.Model.ScopeField]::Environment, (New-Object Octopus.Client.Model.ScopeValue("Environments-41")))
    $variables = $spaceRepository.VariableSets.Get($libraryVariableSet.VariableSetId)
    $variables.Variables.Add($variable1)
    $spaceRepository.VariableSets.Modify($variables)

#Create second variable (I also hard coded some stuff, and changed it to hit spaceRepository. Also, you need the scope to be the environment Id, not the environment name (at least in my testing)
    $Variable1 = New-Object Octopus.Client.Model.VariableResource
    $Variable1.Name = "ArtifactoryLocalRepository"
    $Variable1.Value = "Second Test Value"
    $variable1.Scope.Add([Octopus.Client.Model.ScopeField]::Environment, (New-Object Octopus.Client.Model.ScopeValue("Environments-42")))
    $variables = $spaceRepository.VariableSets.Get($libraryVariableSet.VariableSetId)
    $variables.Variables.Add($variable1)
    $spaceRepository.VariableSets.Modify($variables)

The results for me are:

Please try to adapt the above changes to your script and let me know if it works for you or if you need more help.

As always please thoroughly test any scripts we help you with in a test environment to ensure desired results before running it in production.

Thanks,
Jeremy