Manage (add and especially delete) variables from powershell

Hello,

We want to manage the variables of VariableSets from powershell as each VariableSet will probably contain several hundred variables (for only one environment, we could have up to 30 of them so imagine the number of variables to manage…).

You provide examples of adding and updating variables but I want to be able to delete all variables of a VariableSet to make sure I can import them again from our verified source. I didn’t find a way to delete variables, only VariableSet (which I want to avoid)…

Could you help me on this ?

Thanks,
Thibaut

Hi Thibaut,

Thanks for reaching out! You should be able to do this using Octoposh: https://github.com/Dalmirog/OctoPosh/blob/Development/docs/advancedexamples/modifying-resources.md#clear-a-variable-set-project-or-library

Let me know if that works for you.

Regards,
Dalmiro

Hi Dalmiro,

Thanks it works. I didn’t use OctoPosh directly because I had already written some scripts and I didn’t want to start over again but it’s a great source of inspiration ! :wink:

I only used

  • $variableSet.Variables.clear()
  • $repository.VariableSets.Modify($variableSet)

One thing though, I had to change one piece of code to work: $variableSet.Variables.clear() instead of $variableSet.Resource.Variables.clear(). I’m using Octopus 3.12.0 and Octopus.Client 4.15.3 for info.

Regards,
Thibaut

Hi Thibaut,

Glad to hear its working :).

That makes sense. If you use the client through Octoposh, you’ll need to use $variableSet.Resource.Variables.clear(), but if you use it raw from Powershell, $variableSet.Variables.clear() would be enough.

If you are going to keep using powershell scripts to talk to the Octopus API, I definitely encourage you to try Octoposh. Next time, before you start writing a new script, ask in the Project’s Gitter channel if there isn’t something in Octoposh for what you are trying to do already and I’ll let you know :slight_smile:

Cheers,
Dalmiro