Reading Time: 3 minutes

The other day I was having a look around the Azure VM blade and came across an option called Run Command. I have never seen this option before so of course, I clicked it, and I am glad I did. From inside this section you are able to run scripts and commands on an Azure VM right from the Portal. No need to connect to the VM. This can come in handy for troubleshooting, recovery, and general maintenance. Below I will show you around Run Command and what it can do.

Information

To be able to do this you will need to have the Microsoft.Compute/virtualMachines/runCommand/action permission. This, with the Contributor and higher RBAC roles. You can also assign it with custom RBAC roles.

Lets use the Run Command

Inside the Azure Portal go to a VM you would like to run a PowerShell script or Command on. If you scroll down you will find Run Command under Operations.

Here you will see a list of options. To test that everything works for you, lets start with a simple one. IPConfig. Click IPConfig. A blade will pop up. You can expand the Script section to see what will run. When your happy click Run.

The script will start to execute. You should see a blue banner appear at the top of the blade. You will also see a black Output box appear at the bottom of the blade. Once the script has finished executing you will see some output like in the below image.

You may have to scroll to see all of the output.

Running custom Scripts

If you pick the RunPowerShellScript option you will be able to run your own custom scripts. There are some things to remember when you are running custom scripts and they are:

  • You can only run one script at a time
  • User input is not supported
  • The output is currently limited to the last 4096 bytes
  • It will take at least 20 seconds for your script to run
  • If your script runs longer than 90 minutes it will timeout
  • Once started, you can not stop it from running
  • All Run Commands run as System
  • Outbound connectivity from the VM is required to return the results of the script

Now you are aware of the restrictions let try running a small custom script. In the RunPowerShellScript blade enter a PowerShell command. You can use Get-Process if you would like to test.

A little bit extra!

You can also run a script from your local machine on an Azure VM. One you have logged in to you Azure Subscription you can use the following PowerShell Command.

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

I hope you found this helpful. If you have any questions please reach out in the usual ways.


Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *