如何在Azure CLI命令上的az vm扩展中使用动态变量

如何在Azure CLI命令上的az vm扩展中使用动态变量,azure,azure-virtual-machine,azure-cli,Azure,Azure Virtual Machine,Azure Cli,我想使用Azure CLI命令运行脚本 我使用下面的命令来做这件事 要执行的命令:- az vm extension set --resource-group rg1 --vm-name test --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.0 --protected-settings '{"fileUris": ["https://test.blob.core.windows.net/testco

我想使用Azure CLI命令运行脚本

我使用下面的命令来做这件事

要执行的命令:-

az vm extension set --resource-group rg1 --vm-name test --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.0 --protected-settings '{"fileUris": ["https://test.blob.core.windows.net/testcontainer/test.sh?st=2018-05-28T19%3A56%3A00Z&se=2018-05-30T19%3A56%3A00Z&sp=r&sv=2017-04-17&sr=b&sig=LsAmSVWmggcBYXs7XwUhdA16HfSChi6%2FvH0vqjZMS%2F1YM%3D"], "commandToExecute": "bash test.sh 1 2 3"}'
az vm extension set --resource-group rg1 --vm-name test --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.0 --protected-settings '{"fileUris": [$SCRIPT_LOCATION], "commandToExecute": "bash $SCRIPT_NAME $PARAM1 $PARAM2 $PARAM3"}'
我想使用下面的方法在命令中作为变量传递,并得到以下错误

脚本_位置=”https://test.blob.core.windows.net/testcontainer/test.sh?st=2018-05-28819%3A56%3A00Z&se=2018-05-30T19%3A56%3A00Z&sp=r&sv=2017-04-17&sr=b&sig=LSAMSVWMGGCBYXS7XWUDA16HFSCHI6%2FvH0vqjZMS%2F1YM%3D” SCRIPT_NAME=“test.sh”PARAM1=“test1”PARAM2=“test2”PARAM3=“test3”

要执行的命令:-

az vm extension set --resource-group rg1 --vm-name test --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.0 --protected-settings '{"fileUris": ["https://test.blob.core.windows.net/testcontainer/test.sh?st=2018-05-28T19%3A56%3A00Z&se=2018-05-30T19%3A56%3A00Z&sp=r&sv=2017-04-17&sr=b&sig=LsAmSVWmggcBYXs7XwUhdA16HfSChi6%2FvH0vqjZMS%2F1YM%3D"], "commandToExecute": "bash test.sh 1 2 3"}'
az vm extension set --resource-group rg1 --vm-name test --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.0 --protected-settings '{"fileUris": [$SCRIPT_LOCATION], "commandToExecute": "bash $SCRIPT_NAME $PARAM1 $PARAM2 $PARAM3"}'
错误:-

部署失败。相关ID:72859669-9cdf-4bb0-9aac-1a6af52c7e1c。VM在以下情况下报告了故障: 正在处理扩展“CustomScript”。错误消息:“启用失败: 无法获取配置:json验证错误:无效受保护 设置JSON:FileURI.0:与格式“uri”不匹配


我可以使用以下命令解决上述问题。下面的命令运行良好

az vm extension set --resource-group rg1 --vm-name test --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.0 --protected-settings "{\"fileUris\": [\"$SCRIPT_LOCATION\"], \"commandToExecute\": \"bash $SCRIPT_NAME $PARAM1 $PARAM2 $PARAM3\"}"

这里有一种不同的方式,我喜欢它,因为它避免了转义双引号的需要(因此JSON负载更具可读性):


这似乎是扩展的问题,而不是CLI的问题。如果您使用ARM模板或Powershell尝试此操作,是否会出现相同的错误?对于您的问题,我认为您将自定义脚本存储在Azure存储帐户Blob的容器中。如果出现错误,文件URI可能是错误的点。我使用cli命令进行了测试,URI应该如下所示:。所以,我建议你仔细检查URI。@Charles MSFT我已经解决了这个问题。参考我的答案。@ZahidFaroq我已经解决了这个问题。请参考我的答案