1

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?

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

so interesting when you do it with a var put the var in ' ' doesn't work. So I have had to do a replace and replace all the " with \" then pass it to the command
1

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

2 Comments

Why not accept my answer instead of adding another answer when you just copy the solution?!
The json string must contain backslashes, rather than just escaping the json to fit in the string. The answers are different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.