1

How to pass a value that has ampersand (&) and possibly other characters. the following powershell script fails

 $groupname = "owtest"
 $tags = @()
 $key="test"
 $value="some=&this&that"
 $tags += "$key=$value"
 $creation = az pipelines variable-group create --name $groupname --variables $tags --only-show-errors

Is there a workaround to allow the ampersand embedded character?

user2503078
  • 737
  • 1
  • 8
  • 24

1 Answers1

3

In my test, we must use double quotes(single quotes can't work for this scenario) for value which contains & or other characters. So if you pass variables directly to final command, it should be:

az pipelines variable-group create --name $groupname --variables test="some=&this&that" ...

And if you want to use the format above($tags += "$key=$value"), you need to use $value="""some=&this&that""" instead of $value="some=&this&that".

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • This doesn't work for me with "az pipelines variable-group variable create". I have to enclose each "&" with double quotes, and the entire variable with single quotes, as described here: https://tomssl.com/setting-keyvault-secrets-through-the-azure-cli/ ... But what if there are quotes in the value!? This is crazy, and really needs guidelines documented. – Nick Westgate Mar 18 '21 at 12:17
  • This is also true for setting passwords as secrets (not KeyVault) using variable groups. Was having issues with a password that contains quotes, even when typing it from the UI. Double quotes did the trick although it does seem wrong that way – tgarcia Sep 22 '22 at 20:56