问题描述
如何传递包含&符和其他字符的值。以下Powershell脚本失败
$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
是否存在允许“&”号嵌入字符的解决方法?
解决方法
在我的测试中,对于包含&
或其他字符的值,我们必须使用双引号(单引号无法在这种情况下使用)。因此,如果您将变量直接传递给最终命令,则应为:
az pipelines variable-group create --name $groupname --variables test="some=&this&that" ...
如果要使用上述格式($tags += "$key=$value"
),则需要
使用$value="""some=&this&that"""
代替$value="some=&this&that"
。