如何指定包含&符号的Azure应用程序配置值?

问题描述 投票:0回答:2

我正在使用 Azure CLI 尝试为此 URL 设置应用程序配置值:

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

我得到以下结果。请注意,这是来自 az 命令,而不是来自 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.

是否有某种方法可以让 az 将这个 URL 作为值,而不必诉诸 URL 编码之类的愚蠢的东西?这对我来说似乎很糟糕。

另请注意,消息

Please specify config store name or connection string(suggested).
是因为它无法正确解析命令行,因为这是用 -n 指定的。如果我转移论点:

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

我现在明白了:

{
  "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.

这看起来更糟。它错误地创建了键值对,然后尝试将 param2 作为命令运行。什么鬼?

azure azure-cli
2个回答
2
投票

通过@d-m 的链接之一让我看到了这一点:

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

该链接充分解释了它,但基本上我需要在单引号内使用双引号,因为 Powershell 评估参数,然后 cmd 评估它们。因此,单引号被 powershell 去掉,然后双引号被 cmd 去掉,最后 az 得到了正确的参数。

这有效:

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"
}

1
投票

Azure CLI 默认使用 Bash

&
是 Bash 中的特殊字符,必须转义。

根据这个答案您可以直接使用反斜杠来完成此操作:

"foo\&bar"
或将整个参数用单引号引起来:
'foo&bar'

从 PowerShell 发出任一命令都不会有问题,因为 PowerShell 使用反引号 (

`
) 来转义其特殊字符

© www.soinside.com 2019 - 2024. All rights reserved.