I took a quick look at your script and depending on your version of Octopus, you may need to increment version in the json body before doing the PUT call. Can you please implement that logic, try it again and let me know if it works?
Thanks for writing. You mean to create an empty array and then do the increment version like this snippet below snipprt , if yes then i am getting error and respective array doest now have op_Addition function.
# Get List Of Existing ProjectVariableSet Variables
$OctopusProjectVariables = @()
$OctopusProjectVariables = Get-OctopusProjectVariables -OctopusDeployProject $OctopusProject -OctopusServerUrl $OctopusServerURL -OctopusServerApiKey $OctopusServerApiKey -SpaceId $SpaceId
# Delete Variable
$VariableToRemove = ($OctopusProjectVariables.Variables | Where-Object { $_.Name -eq "EnvironmentName" })
if ($null -eq $VariableToRemove) {
$OctopusProjectVariables.PSCustomObject.Properties.Remove('$VariableToRemove')
}
# Construct Payload Of JsonBody Over Invoke-RestMethod Session
$OctopusProjectVariables += $OctopusProjectVariables
$JsonBody = $OctopusProjectVariables | ConvertTo-Json -Depth 10
Sorry about the confusion. What I meant was in that json body that you’re bringing down with the variables then putting back up with a PUT call, there is a “version” field. It will need to be incremented once before the API call will work. If it’s 7, make it 8, etc. I’m not sure on the syntax but you should be able to do a dot operator on the version and ++ it.
What is the response code you’re getting when you make the call?
I have updated the snippet, but I guess I am too not getting the System.Int32 member type with an increment. I am missing some logic of iteration . Here is the snippet :
I think the problem lies in how you’re removing the variable. That properties.remove command removes a root level property from the object. The JSON body has the properties: Id, OwnerId, Version, Variables, ScopeValues, SpaceId, and Links. You could use that command to remove an entire section of those.
I’m not familiar with this method of deletion from a powershell object, but my gut tells me you’d have to somehow go one layer deeper. You may need to create a powershell object out of the variables property by themselves, then run the remove on that object, then put the variables back inside the rest of the object with the rest of the properties.
Thanks for the response. I need to look into it of creating PSObjects for deletion and getting back the rest of the objects as you have suggested. I will update the thread tomorrow after some analysis.
In Mean while : I was trying the another method with Octopus.Client.dll which is not throwing the any error however not working as well :
I think the problem is this line: $Variableset.Variables.Remove($VariableToRemove)
You are creating it with only a variable name, not all of the contents. I believe they need to be identical resources.
Here is my working code. I had to add in some stuff since I work with spaces so the beginning is a bit different, but the part I think you will need to modify is the third to last line. $VariableToRemove = $variableset.Variables | Where-Object {$_.Name -eq $VariableName}
I was not able to able thread the thread, I have used the Octopus.Client.dll to delete variable along with whole code of REST Method. Actually, it was bit tricky & time consuming for me to understand how I can delete single project variable using REST Method. You can think on this to work if you want and I will too do to explore about it.
Function Remove-Variable {
param(
$VariableSet,
$VariableName
)
$tempVars = @()
foreach ($variable in $VariableSet.Variables){
if ($variable.Name -ne $VariableName){
$tempVars += $variable
}
}
$variableset.Variables = $tempVars
}
It worked for me in my initial testing. The logic is that it creates a temporary array of variables by skipping the variable we want to remove when iterating and copying everything over. After it has created that temporary set, we just copy the set to the original’s variable array.
If you do use this please use thorough testing as I just checked if it removes the variable, I didn’t test anything outside of that. It may cause adverse effects on the variable set in ways I didn’t anticipate in your scenario.