4

I have found that azure python sdk has provided following method for running command in linux vm.

from azure.mgmt.compute import compute_management_client
from azure.common.credentials import ServicePrincipalCredentials

credentials = ServicePrincipalCrendentials(client_id, secret, tenant)
client = compute_management_client(credentials, subscription_id)

client.virtual_machines.run_command(resource_group_name,
     vm_name, parameters, customheaders=None, raw=False,
     **operation_config)

But how do I pass my command here? I couldn't find any sample for parameters and operation_config. Please Help

Suraj Shrestha
  • 728
  • 11
  • 19

1 Answers1

10

Basic example:

  run_command_parameters = {
      'command_id': 'RunShellScript', # For linux, don't change it
      'script': [
          'ls /tmp'
      ]
  }
  poller = client.virtual_machines.run_command(
        resource_group_name,
        vm_name,
        run_command_parameters
  )
  result = poller.result()  # Blocking till executed
  print(result.value[0].message)  # stdout/stderr

If you wish to inject parameters, you can do this:

    run_command_parameters = {
        'command_id': 'RunShellScript',
        'script': [
            'echo $arg1'
        ],
        'parameters':[
            {'name':"arg1", 'value':"hello world"}
        ]
    }

If using Windows, you can use RunPowerShellScript command id

You might want to test your command using the CLI: az vm run-command invoke --help

Since the CLI is using this SDK, you'll get the same behavior.

Laurent Mazuel
  • 3,422
  • 13
  • 27
  • that helped. thank you . So things possible using SDK is greater than CLI,but the documents are mostly for CLI. Can we find examples of SDK for respective CLI command in github? – Suraj Shrestha Jul 24 '18 at 03:37
  • I'd start with https://learn.microsoft.com/python/azure/, which is probably the best starting python, and then on https://github.com/Azure-Samples/. Not everything is documented yet, we working on it actively. – Laurent Mazuel Jul 24 '18 at 16:25