I just posted this, but when I hit create it said “awaiting moderation” and now I cannot find any evidence of my post…if this is a double post, please delete one of them.
When using the octopus REST powershell script to update a variable, powershell is:
Outputt JSON that octopus doesn’t support -> HTTP 500
Unable to ‘ingest’ JSON from octopus web client -> locks powershell
There may have been an error with your original post to do with the attachment you included. We think it is an issue with the forum software we are using and that it could be related to the file type of your attachment. Sorry about that!
With regards to your issue, I think you have encountered one of the interesting quirks of Powershell
One simple solution is to serialize (ConvertTo-Json) the SyncRoot object on PsMemberSet, instead of the whole PSMemberSet object. In the script you provided, this means replacing this line:
$variableset.Variables = $newvars
with this one
$variableset.Variables = $newvars.SyncRoot
Another option would be to manipulate the $variableSet.Variables directly, instead of doing extra round-trip serialization of this collection with the $newvars variable. This should bypass any JSON conversion problems entirely.
Going back through the depths of my memory to last Friday…ha.
The reason I was doing the double-hop conversion is due to my lack of understanding with powershell. If I simply do the invoke-restmethod, that’s gets the data fine…but I can’t modify it (see attached).
However, if I converto-json then convertfrom-json, I can then edit the value.
It’s weird because I can perform a subtraction the value, but can’t assign it something else.
Once I figure that out, the rest should work fine as you said. Cheers.
What I noticed was that powershell was doing funny things with type, and the type for the variable data was ‘pscustomobject’, which powershell was funnily either interpreting as a single string, or as an actual custom object, depending on how it felt at the time.
But then if you did some variable assignment, powershell would recognize the individual fields…so this showed it sort of half-worked. From there, I figured if I could convert to an array, that might work.
This worked perfectly (attached).
Once done, the variable updated fine in octo.
Thanks for that, yet again I learned that powershell customobjects are a super big pain in the proverbial to deal with. :).