Run PowerShell scripts in your Windows VM with Run Command

Sometimes, it can be helpful to run a command in a Virtual Machine located on Azure in order to remediate a Virtual Machine that doesn’t have the RDP port open. To perform this task, you must use the “Run Command” feature.

How does it work?

Run Command uses the VM agent to let you run a script inside this virtual machine. This can be helpful for instance when you need to create a local admin account even if you don’t have access to the Virtual Machine.

Example

First, you must start the VM in order to perform this task. Then, go to the Virtual Machine and click “Run Command”:

As you can notice, there is a list of the available commands to run on the VM. In my case, I will use the “RunPowerShellScript” command. Here, you can copy/paste your script but be careful because scripts that prompt for information (interactive mode) are not supported.

$Password= ConvertTo-SecureString “P4ssword12345” -AsPlainText -Force
New-LocalUser “GET-CMD” -Password $Password -FullName “GET-CMD” -Description “GET-CMD”
Add-LocalGroupMember -Group “Administrators” -Member “GET-CMD”

Now, run the script and check the output:

Done! The account has been created and added to the Administrators local group.

Thanks for reading! You can follow me on Twitter @PrigentNico

About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.