Copy variable from project to library not working

I have created a simple script to add a project’s all variable to library. This is working for a single variable but not working for multiple variable. The for loop is failing with this error:

"Exception calling “Modify” with “1” argument(s): "There was a problem with your request.

  • Changes to these variables could not be saved, because another user has made changes to the variables between when you started editing and when you saved your changes. Please reload or open a new tab to make your changes."UpdateLibraryVariable.ps1 (1.2 KB)

Hi @arka.biswas,

Thanks for getting in touch!

You almost have it spot on, just need one minor amendment to your script. The modify line needs to come at the end, outside of the loop. This way, all of your changes are added to the $variables object and then they are committed in bulk with the modify.

e.g.

foreach($variable in $projectVariables.Variables)
{ 
   $myNewVariable = new-object Octopus.Client.Model.VariableResource
   $myNewVariable.Name = $variable.Name
   $myNewVariable.Value = $variable.Value
   $variables.Variables.Add($myNewVariable)
}

$repository.VariableSets.Modify($variables)

Let me know if this works out for you.

Regards,
Paul

1 Like

Aha!..Works like a charm…Thanks a lot.

1 Like

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