Groovy:执行带有JSON解析问题的azure CLI命令?

Groovy:执行带有JSON解析问题的azure CLI命令?,azure,groovy,azure-cli,groovyshell,Azure,Groovy,Azure Cli,Groovyshell,目前我无法从Groovy运行azure CLI命令,因为该命令中有JSON部分 有一个azure命令可在虚拟机上运行自定义脚本。CommandToExecute在机器上作为JSON传递 工作示例: REQUEST-CALL in Console:az vm extension set -g demo --vm-name demo-cfg01 --name CustomScript --publisher Microsoft.Azure.Extensions --settings '{"comma

目前我无法从Groovy运行azure CLI命令,因为该命令中有JSON部分

有一个azure命令可在虚拟机上运行自定义脚本。CommandToExecute在机器上作为JSON传递

工作示例:

REQUEST-CALL in Console:az vm extension set -g demo --vm-name demo-cfg01 --name CustomScript --publisher Microsoft.Azure.Extensions --settings '{"commandToExecute":"ls"}'

RESPONSE: {
  "autoUpgradeMinorVersion": true,
  "forceUpdateTag": null,
  "id": "/subscriptions/xxxxxxxxxx-xxxxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/demo/providers/Microsoft.Compute/virtualMachines/demo-cfg01/extensions/CustomScript",
  "instanceView": null,
  "location": "germanycentral",
  "name": "CustomScript",
  "protectedSettings": null,
  "provisioningState": "Succeeded",
  "publisher": "Microsoft.Azure.Extensions",
  "resourceGroup": "demo",
  "settings": {
    "commandToExecute": "ls"
  },
  "tags": null,
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "typeHandlerVersion": "2.0",
  "virtualMachineExtensionType": "CustomScript"
}
这个脚本很好用

使用Groovy执行的“相同”命令会导致以下结果:

def process
        StopWatch.withTimeRecording("EXECUTING COMMAND '" + cargs + "'",_logger, Level.ALL) {
            process = (cargs).execute(null,null);
            process.waitForProcessOutput(sout, serr)
        }
请注意秒表,它记录包含参数的StringArray:

正在执行命令'[az,vm,extension,set,-g,demo,--vm name, demo-cfg01,-名称,自定义脚本,-发布者, Microsoft.Azure.Extensions,--设置,'{“commandToExecute”:“ls”}']

参数看起来与控制台中的相同

Azure的响应是:

VM在处理扩展“CustomScript”时报告失败。 错误消息:“启用失败:获取配置失败:错误” 读取扩展配置:分析设置文件时出错:错误 正在解析json:json:无法将字符串解组为类型为的Go值 映射[字符串]接口{}


我认为groovy在执行之前不知怎么逃逸了字符,我无法找出哪里出了问题。有什么建议吗?

当您在
数组上调用
执行
时,groovy(实际上是java)会双引号引引引每个参数

只需根据需要在字符串中构建命令行

groovy中的字符串具有与数组相同的执行方法

def cmd = """az vm extension set -g demo --vm-name demo-cfg01 --name CustomScript --publisher Microsoft.Azure.Extensions --settings '{"commandToExecute":"ls"}' """
def process = cmd.execute()

当您使用字符串execute时,groovy将执行您提供的确切命令。找到了一个“解决方法”。az命令还接受一个*.json文件作为设置参数。因此,我首先在一个临时json文件中创建该命令,并将json文件作为参数传递。Works!

您引用的
execute()
call。您不需要在此处引用,因为此处不涉及shell或命令解析器

您在那里的命令将获得
'{“commandToExecute”:“ls”}'
,这是一个有效的JSON字符串(无映射),这也是错误消息所述的内容:

解析json时出错:json:无法将字符串解组为map[string]接口类型的Go值


只需使用
{“commandToExecute”:“ls”}
(没有周围的
)作为参数。

遗憾的是,这并不能解决问题-出于对你理智的热爱,同样的结果是,不要使用
“string”。execute()
(ever)-重写它,这样做
[“az”,“vm”,“…”)。execute()
并且不要为从未涉及的shell引用参数。如果您需要shell语句,请使用
[“sh”、“-c”、“am-vm…”。execute()
@cfrick,我不同意。请查看字符串数组的构造函数(第151行),当您使用
[]时,最后会调用它。execute()
。它构建单字符串命令行并引用一些数组元素。在某些情况下,特别是在ms工具中,您不需要这些引号…@daggett它在jdk8中不需要,即使它仍然需要,这不是您的问题。如果您引用和JDK引号,那么它又错了。Groovy使用
.execute()拆分字符串
在空白和任何引用上,它都不会阻止它。解析问题来自Azure。如果我在客户端留下“命令失败,因为Json没有被识别为正确的参数……那么这个工具就坏了,你应该在那里创建一个票证。如果他们声称它没有被窃听,我会告诉他们好运与m让unix用户理解这样的行为;P