
close
close
This post will show how to use the Run Command to execute PowerShell scripts inside an Azure virtual machine running Windows Server from the Azure Portal. Note that this can also be done using PowerShell, CLI, and REST API.
I often find myself in situations where I want to get something done with a virtual machine that I have access to via the Azure Portal but I haven’t got network access to it. I could configure network access and RDP into the virtual machine but that will either require time or an unwanted configuration change. I could use the Serial Access Console to log into the machine and get a PowerShell user interface but that’s more of a back door. What if I just want to run a one-off PowerShell script to get something done?
Microsoft added a new method for executing PowerShell scripts inside of a virtual machine in a simple, on-demand basis, called the Run Command. At any time, I can execute one of a set of supplied scripts or a custom script on one of several ways:
This method requires that you have a certain level of permissions for the virtual machine in question. You must have the Microsoft.Compute/virtualMachines/runCommand/action permission, which is available to administrators with Contributor or higher rights over the virtual machine resource.
You can find Run Command in the settings of a virtual machine under Operations.
Virtual Machine Run Command in the Azure Portal [Image Credit: Aidan Finn]
Running the Supplied IPConfig Script in an Azure Virtual Machine [Image Credit: Aidan Finn]
Executing Customer PowerShell Commands or Scripts Inside an Azure Virtual Machine [Image Credit: Aidan Finn]
Invoke-AzureRmVMRunCommand -ResourceGroupName 'petri' -Name 'vm-petri-01' -CommandId 'RunPowerShellScript' -ScriptPath 'myscript.ps1'
There are some restrictions when you use Run Command:
Some early feedback to Microsoft has found that Run Command requires that the virtual machine has some level of outbound connectivity to the Internet, probably to supply the output from an extension to Azure.
More in Microsoft Azure
Microsoft Dev Box Adds New Features Ahead of its General Availability in July
May 23, 2023 | Laurent Giret
Microsoft's Azure Deployment Environments Service Now Generally Available
May 23, 2023 | Rabia Noureen
Microsoft Fabric Brings New AI-Powered Data Analytics Features to Boost Productivity
May 23, 2023 | Rabia Noureen
Azure Firewall Adds New Logging and Metrics Features to Enhance Traffic Analysis
May 17, 2023 | Rabia Noureen
Most popular on petri