Blog Post

ITOps Talk Blog
2 MIN READ

How to run scripts in your Azure VM by using Run Command

thomasmaurer's avatar
thomasmaurer
Icon for Microsoft rankMicrosoft
May 06, 2020

As you know, you can access your Azure virtual machine (VM) in multiple ways, like SSH or RDP. However, if you have issues with the RDP or SSH network configuration, the Run Command feature is another option. Run Command can run a PowerShell or shell script within an Azure VM remotely by using the VM agent. This scenario is especially useful when you need to troubleshoot operating system network configurations or user access configuration.

 

 

You use Run Command for Azure VMs through the Azure portalREST API, Azure CLI, or PowerShell. Here are some examples:

 

Azure Portal

You can run commands directly from the Azure Portal. In the menu of the Azure VM, you can select Run command. Here you can find some predefined scripts to troubleshoot your Azure VM. In the case of a Windows VM, you will find scripts like configuring RDP port or enable PowerShell remoting. But you can also run your custom PowerShell script.

 

Azure VM Run Command Run PowerShell Script

 For Linux VMs, you will find predefined options to run a Linux shell script or ifconfig to list the network configuration.

 

Azure CLI

You also run commands directly from the Azure CLI. 

 

 

 

 

 

 

 

 

az vm run-command invoke -g myResourceGroup -n myVm --command-id RunShellScript --scripts "sudo apt-get update && sudo apt-get install -y nginx"

 

 

 

 

 

 

 

 

The az vm run-command CLI command works with Linux and Windows VMs. You can find more information about the Azure CLI option here.

 

Azure PowerShell

You can also use Azure PowerShell to use the run command capabilities to run PowerShell scripts against the guest agent inside the Azure VM. For that, you can simply use the Invoke-AzVMRunCommand cmdlet from the Az PowerShell module. You can also run this command directly from Azure Cloud Shell as well.

 

 

 

 

 

 

 

 

Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter @{"arg1" = "var1";"arg2" = "var2"}

 

 

 

 

 

 

 

 

The cmdlet expects the script referenced in the -ScriptPath parameter to be local to where the cmdlet is being run. If you are running it from your local machine, the script will need to be stored on your machine. If you are running the script from Cloud Shell, you will need to have the script available in your Cloud Shell. You can find more information about the Invoke-AzVMRunCommand PowerShell cmdlet option here.

If you want to run it against multiple Azure VMs, check out my blog post here.

 

Conclusion

The Run Command option is a great solution if you need to run scripts inside an Azure VM using the guest agent to troubleshoot network or access configurations. You can learn more about the Run Command here:

You can also find more information about the Azure Virtual Machine guest agent here:

I hope this blog post was helpful. If you have any questions or comments, feel free to leave a comment.

 

 
Updated May 11, 2021
Version 5.0
  • That is really cool. By chance, is there a way to call scripts remotely (for example, using gist)?

  • sandeepGzb's avatar
    sandeepGzb
    Copper Contributor

    How to Run scripts on Azure Virtual Machines with the Run Command using Rest API 

    i just want to call script dynamically, i mean  need to pass script name and parameters  

    anyone, please help 

  • If you want to use REST API, look at the spec to construct your URL and json body:
    https://github.com/Azure/azure-rest-api-specs/blob/6b894714b047009e699fe48dada29106f328c8e8/specification/compute/resource-manager/Microsoft.Compute/stable/2021-07-01/runCommands.json#L944

    Example:

    POST /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/runCommand

    {

      "commandId": "RunPowerShellScript",

      "script": [

        "param(",

        "    [string]$first,",

        "    [string]$second",

        ")",

        "Write-Host This is a sample script with parameters $first $second",

        "Write-Host Done."

      ],

      "parameters": [

        {

          "name": "first",

          "value": "value1"

        },

        {

          "name": "second",

          "value": "value2"

        }

      ]

    }

  • sandeepGzb's avatar
    sandeepGzb
    Copper Contributor

    {
    "commandId": "RunPowerShellScript",
    "parameters": [
    {
    "name": "first",
    "value": "value1"
    }
    ],
    "script": [
    "c:\\testps.ps1 "
    ]
    }

  • sandeepGzb's avatar
    sandeepGzb
    Copper Contributor

    above code not worked for me ,unable to get parameter in remote vm 

     

  • If you want to pass a script you need to have the script content set in the script property (instead of a file name):
    "script": ["Write-Host Sample script"]

    The way you use it it tried to run a file that is already on the VM. In this case you need to pass the parameters:

    "script": [
    "c:\\testps.ps1 first=$first"
    ]

  • sandeepGzb's avatar
    sandeepGzb
    Copper Contributor

    finally it worked for me 

    {
      "commandId""RunPowerShellScript",
      "script": [
        "c:\\testps.ps1 -First sandeep -Second sharma"
      ]
    }
    now I want to get data from the output for the message: value please help. I working on the logic app and want to use it in the next step. please help 
    {
      "value": [
        {
          "code""ComponentStatus/StdOut/succeeded",
          "level""Info",
          "displayStatus""Provisioning succeeded",
          "message"" sandeep   ---  sharma  "
        },
        {
          "code""ComponentStatus/StdErr/succeeded",
          "level""Info",
          "displayStatus""Provisioning succeeded",
          "message"""
        }
      ]
    }
     
  • Dave Thomas's avatar
    Dave Thomas
    Copper Contributor

    Hello all, I have a new user and when I wanted them to run a command, RDP Listening one, they get a pop up at the top of the screen.  

    I was thinking this might be a role issue, but I am not sure on what directory role they would need.  Does anyone know, or am I looking in the wrong direction ?

     

    Thank you,

    Dave