There are several ways to run commands and scripts on Azure VMs depending on the design you need to implement. You can use runbooks, Desired State Configuration (DSC) scripts, Azure DevOps pipelines, and many other third-party solutions for this. There's also a pretty simple standalone solution to execute commands on Azure VMs, which is a built-in feature in Azure Portal and is also usable through PowerShell.

We have two options here. First, we can use Azure Portal to execute predefined, custom PowerShell commands on a VM using the Virtual machine blade in Azure Portal, shown below.

Run a command from the Virtual machine blade in Azure Portal

Run a command from the Virtual machine blade in Azure Portal

We can execute predefined PowerShell scripts in the portal manually to take quick actions whenever needed, such as enabling an admin account or setting a custom RDP port. On the other hand, we may still want to automate all of these script executions, especially when we need to run commands on multiple VMs without having to do so one by one.

There are basically two cmdlets to manage running commands on Azure VMs. The first one lets us see the run command documentation. This clearly shows the command IDs and explains the predefined scripts. The following cmdlet shows the predefined commands available in a specific region.

Get-AzureRmVMRunCommandDocument -Location NorthEurope | select ID, OSType, Label

All available predefined scripts for both Windows and Linux machines in a specific region

This cmdlet just shows all available predefined commands in the North Europe region. Even though these commands can be pretty useful, the RunPowershellScript command is the most flexible one to use. It allows us to run any custom PowerShell script that we specify on a VM.

Executing predefined PowerShell scripts ^

Now let's take a short tour around these predefined commands and then try to run our own script on a VM. The following command executes the predefined PowerShell script called EnableRemotePS.

Invoke-AzureRmVMRunCommand `
        -CommandId "EnableRemotePS" `
        -ResourceGroupName "RunCommandTestRG" `
        -VMName "testvm0001"
Enabling remote PowerShell on a VM

Enabling remote PowerShell on a VM

The following command gets the current RDP settings from a VM:

Invoke-AzureRmVMRunCommand `
        -CommandId "RDPSettings" `
        -ResourceGroupName "
Getting RDP settings from a VM

Getting RDP settings from a VM

The following command simply changes the RDP port a VM uses, which is 3389 by default. In my scenario, I will change it to 3399 instead of 3389.

Invoke-AzureRmVMRunCommand `
        -CommandId "SetRDPPort" `
        -ResourceGroupName "RunCommandTestRG" `
        -VMName "testvm0001" `
        -Parameter @{"RDPPort"=3399}
Setting up a custom RDP port on a VM

Setting up a custom RDP port on a VM

If we need to enable a built-in admin account on a VM, we can use the following:

Invoke-AzureRmVMRunCommand `
        -CommandId "EnableAdminAccount" `
        -ResourceGroupName "RunCommandTestRG" `
        -VMName "testvm0001"
Enabling a built in admin account on a VM

Enabling a built in admin account on a VM

This is another useful command to reset RDP certificate settings on a VM:

Invoke-AzureRmVMRunCommand `
        -CommandId "ResetRDPCert" `
        -ResourceGroupName "RunCommandTestRG" `
        -VMName "testvm0001
Resetting RDP certificate settings on a VM

Resetting RDP certificate settings on a VM

Executing custom PowerShell scripts ^

It's time to execute our own PowerShell scripts on a VM using the Invoke-AzureRmVMRunCommand cmdlet. The main difference between executing custom scripts and predefined scripts is the local PowerShell script file to execute on an Azure VM.

On my local computer, we'll run this sample PowerShell script on the VM named "testvm0001."

A sample PowerShell script

A sample PowerShell script

When we run the following command, the PowerShell console can show us the output of the command:

Invoke-AzureRmVMRunCommand `
        -CommandId "RunPowerShellScript" `
        -ResourceGroupName "RunCommandTestRG" `
        -VMName "testvm0001" `
        -ScriptPath "C:\temp\script.ps1"
The output of a PowerShell script on a VM

The output of a PowerShell script on a VM

Restrictions ^

There are some limitations and restrictions defined below.

Subscribe to 4sysops newsletter!

  • There's no support for the interactive mode. This means there's no support when the script you want to run on a VM needs a user to enter any value or confirm the execution.
  • We can only run one script at a time on a VM.
  • The maximum output size is four kilobytes.
  • It works a bit slowly because it needs time to prepare its own environment to execute our scripts on a specific VM. It takes a minimum of 20 seconds to run a script.
  • Scripts use the System account on Windows operating systems.
  • It is not possible to cancel a script after executing it. So it is important to double-check it before we kick it off.
  • The script times out after 90 minutes, so we need to make sure it does not take much time.

Conclusion ^

It is sometimes extremely important to run a PowerShell command on a VM in the quickest way. This cmdlet lets us run almost any PowerShell script on a particular VM without having to use custom script extensions, DSC scripts, or automation accounts.

3 Comments
  1. Sameer 2 years ago

    good post but please can you share other options as well

  2. Hi Sameer,
    Here are some of the Azure built-in solutions to perform the task. Also, there are 'n' number of third-party tools as well. 
    https://docs.microsoft.com/en-us/azure/virtual-machines/windows/run-command
    https://docs.microsoft.com/en-us/azure/virtual-machines/windows/run-scripts-in-vm

  3. Graham Hardie 1 year ago

    Great article!  Just what I needed.  

Leave a reply

Please enclose code in pre tags

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

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account