2

I'm using Azure CLI to try to set an app config value to this URL:

az appconfig kv set --key "mykey" --value "https://fake.com/action?param1=a&param2=b" -n "test" --yes

I get the following result. Note that this is coming from the az command, not from the shell.

Please specify config store name or connection string(suggested).
'param2' is not recognized as an internal or external command,
operable program or batch file.

Is there some way to get az to take this URL as the value without having to resort to stupid stuff like URL encoding it? This seems broken to me.

Also note, the message Please specify config store name or connection string(suggested). is because it's failing to parse the command line properly because this is specified with -n. If I move the arguments around:

az appconfig kv set -n "test" --yes --key "mykey" --value "https://fake.com/action?param1=a&param2=b"

I now get this:

{
  "contentType": null,
  "etag": "zuspBqPBOHR1mXfcKCgORXHpAxB",
  "key": "mykey",
  "label": null,
  "lastModified": "2021-08-31T17:39:14+00:00",
  "locked": false,
  "tags": {},
  "value": "https://fake.com/action?param1=a"
}
'param2' is not recognized as an internal or external command,
operable program or batch file.

Which seems worse. It created the key value pair incorrectly then tried to run param2 as a command. WTF?

Skrymsli
  • 5,173
  • 7
  • 34
  • 36

2 Answers2

2

Following one of the links from @d-m got me to this:

https://github.com/Azure/azure-cli/blob/dev/doc/quoting-issues-with-powershell.md

That link fully explains it, but basically I need to use double quotes inside of single quotes because Powershell evaluates the arguments and then cmd evaluates them. So the single quotes are stripped off by powershell, then the double quotes are stripped by cmd and finally az gets the right argument.

This works:

az appconfig kv set -n "test" --yes --key "mykey" --value '"https://fake.com/action?param1=a&param2=b"'
{
  "contentType": "text/plain",
  "etag": "XugiNgxa2I04w0gaG2OBh9Jcj75",
  "key": "mykey",
  "label": null,
  "lastModified": "2021-08-31T18:20:44+00:00",
  "locked": false,
  "tags": {},
  "value": "https://fake.com/action?param1=a&param2=b"
}
Skrymsli
  • 5,173
  • 7
  • 34
  • 36
  • 1
    Good find, even if the solution is a bit arcane. – D M Aug 31 '21 at 19:00
  • Here's what I had to do to call my `az` command: `az deployment group create --resource-group MyResource --template-file azuredeploy.json --parameters '"WEBSITE_RUN_FROM_PACKAGE='$WEBSITE_RUN_FROM_PACKAGE'"'`. I guess the more correct approach would be to use the Az powershell cmdlets instead of az.exe. – Benrobot Oct 21 '21 at 20:34
1

The Azure CLI uses Bash by default.

& is a special character in Bash and must be escaped.

According to this answer You can do this directly with a backslash: "foo\&bar" or by wrapping the whole argument in single quotes: 'foo&bar'.

There shouldn't be a problem issuing either command from PowerShell since PowerShell uses backticks (`) to escape its special characters.

D M
  • 5,769
  • 4
  • 12
  • 27
  • I have tried single quotes. It behaves the same as double quotes. I have tried escaping with a backslash: az appconfig kv set -n "test" --yes --key "mykey" --value 'https://fake.com/action?param1=a\&param2=b' { "contentType": null, "etag": "GWMmummZfhQqDfDDvLBJDHoeq7s", "key": "mykey", "label": null, "lastModified": "2021-08-31T17:59:57+00:00", "locked": false, "tags": {}, "value": "https://fake.com/action?param1=a\\" } 'param2' is not recognized as an internal or external command, operable program or batch file. – Skrymsli Aug 31 '21 at 18:00
  • 1
    This seems like a bug to me. – Skrymsli Aug 31 '21 at 18:01