0
votes

I am trying to update Azure pipeline group variable from within a PowerShell script using the az cli.

I have the command updating the variable but its modifying the json and removing all the quotes (")

so instead of updating variable from {"a": "a"} to {"a": "b"}

it updates it to {a: b}

I am running the following command

$json = ConvertTo-Json $a -Compress
Write-Host $json
az pipelines variable-group variable update --group-id 236 --name MyJson --detect true --secret false --value $json

The outut of the write-host is

{"a": "b"}

Output of the the az pipelines variable-group variable update is

{
 "MyJson": {
   "isSecret": null,
   "value": "{a:b}"
  }
}

What am I doing wrong and how can I get it to update without modifying the JSON?

2

2 Answers

1
votes

All the variables are stored as strings and are mutable, and it already means string when it's in the double-quotes. So it removes all the quotes inside. If you want to make the quotes also stores as the value, then you need to transfer them like this:

{\"a\":\"b\"}

Or just use single quotation marks:

'{"a":"b"}'

Finally, it will look like this:

enter image description here

It's not necessary to store the double-quotes as the value. So I don't recommend you do this without any special reason.

1
votes

So while @charles works when passing the data straight to the command like that, we are using variables.

While I understand what's going on here, still think the az command could handle this better.

So to get this to work I had to do a replace on the string and replace all " with \"

Annoying that this has to be done but it works, so my final commands look like this:

$new_json = $json.replace("`"", "\`"")
az pipelines variable-group variable update --group-id $(VarGroupId) --name var_name --detect true --secret false --value $new_json