Search code examples
azureazure-virtual-machineazure-rest-api

Need to understand Run command in Azure via REST API


I was following this documentation and trying to run a simple shell script in a vm using this.

https://learn.microsoft.com/en-us/rest/api/compute/virtual%20machines%20run%20commands/runcommand#runcommandinputparameter

But what needs to be the content of the body of the post request is not clear. The commandID can be RunShellScript but where does we provide the script value.

I have tried a body like this

{
    commandId: "RunShellScript",
        script: "/path/scriptname"
}

with other options

script: 'scriptname'
script: 'sh scriptname'

and other each resulting in

{
"error": {
"code": "BadRequest",
"message": "Error converting value "/home/admin1/quick-python-test.sh" to type 'System.Collections.Generic.List`1[System.String]'. Path 'script', line 3, position 52.",
"target": "runCommandInput.script"
}
}

Can anyone help me how to do it properly? I am new to Azure.


Solution

  • To run the bash script in the VM through Azure REST API, here is the sample body for the request:

    {
      "commandId": "RunShellScript",
      "script": [
        "echo $arg1 $arg2"
      ],
      "parameters": [
        {
          "name": "arg1",
          "value": "hello"
        },
        {
          "name": "arg2",
          "value": "world"
        }
      ]
    }