Azure Tips & Tricks - Quickly run commands or script on an Azure Virtual Machine (VM) without logging into the VM

@20aman    Jan 10, 2019

When you are working with Azure Virtual Machines (VM), multiple times you will need to run some command on the VM. For this, you will need to connect to the VM, login and then open the console to run the command and then actually execute it. Instead of going through the trouble of connecting to the VM and logging in during the connection process, you can simple Run the command on the VM directly from the Azure portal.

Note 1: This feature uses the Azure VM Agent on the VM and will not work if the agent is missing.

To leverage this option navigate to your VM in the portal and select the option to "Run Command" under "Operations" section of the VM.

Run Command Option

As shown above, you can Run Complete PowerShell script right from this screen. There are various options for common scenarios like checking IP Config which simply executes "ipconfig /all". You can check these common scenarios and the underlying script by clicking on them. If you want to execute then simply click "Run" in the popup blade.

Running common scenarios

Note 2: You will need to wait for the Output to appear. It takes some time and you just have to be patient. Once you click run, it gets disabled and the operation is running. Once the operation is complete you will see the output in the window. Do not navigate away from this popup blade/window.

Example of Running PowerShell Script

Let us say that you want to install IIS on the VM for quick testing. This can be done easily by running the below PowerShell:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

Click on the "RunPowerShellScript" option. In the popup blade, type the script and hit Run. Wait for the output to appear. Do not navigate away from the window. You can check the status as indicated below.

Running any PowerShell Script

That's all there is to it. For more information you can check the official documentation that can be found here: Run PowerShell scripts in your Windows VM with Run Command





Comments powered by Disqus