DevOps | Scripts | Automation

Powershell

How to use PowerShell RunCommand on Azure VM?

  • If you ever wondered how we can run the script directly on Azure VMs so we don’t need to worry from running script inside the windows server, test ping or WINRM connectivity.
  • If you are Azure Admin or developer and If your organization has requirement to run script on all the running servers of that subscription or on some resource groups.

Then follow this article.

What is the PowerShell Extension in Azure ?

PowerShell Extension or PowerShell RunCommand for Azure VM is to run PowerShell scripts without login into the Azure VMs. Let’s check on the Azure Portal, what does that mean.

Open Azure Portal -> Select Virtual Machine -> Filter “Run Command” -> RunPowerShellScript.

PowerShell Script Extension

Once you Click on RunPowerShellScript it will prompt for the command window and there you can execute commands as shown below. Here the RunPowerShellScript is called the CommandID.

Command Window

This is the way you can run PowerShell script on Azure VMs without login into the machine but you should have subscription access. Before we check how we can use PowerShell to call this extension we need to learn some Benefits and Restrictions of this command and the same has been mentioned on the Microsoft Site.

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/run-command

Running script on Azure VM using PowerShell

As we have seen above, we can use the Azure VM run command to run the script on the Virtual Machine using Portal. Same has been possible through the PowerShell using Invoke-AzVMRunCommand. This command invokes the script that needs to be executed on the Azure VM.

Suppose you are already connected to the Azure Account using PowerShell in the specific subscription (If not use Connect-AzAccount to connect the Azure Cloud and the Set-AzContext to set the Azure Subscription).

We will create a sample PowerShell script which get all the Services which are disabled on the system and we need to run this script on the Azure VM. The below is our sample script. This script is stored in C:\Temp as GetDisabledServices.ps1

Get-Service | where{($_.Status -eq "Stopped") -and ($_.StartType -eq "Disabled")} `
    | Select Name, StartType, Status

When you create a script you need to make to run directly on Azure VM you need to make sure you just write a script for the local machine not for the remote machines so don’t include remoting commands.

Now we have the below Azure VM.

$vm = Get-AzVM -Name TestVM2k19

We will use Invoke-AzVMRunCommand to run GetDisabledServices.ps1 on the Azure VM.

Invoke-AzVMRunCommand -VMName $vm.Name -ResourceGroupName $vm.ResourceGroupName -ScriptPath C:\Temp\GetDisabledServices.ps1 -CommandId RunPowerShellScript

Output:

VM Command Output

If you need the output of the script then use the Value[0] property.

$output = Invoke-AzVMRunCommand -VMName $vm.Name -ResourceGroupName $vm.ResourceGroupName -ScriptPath C:\Temp\GetDisabledServices.ps1 -CommandId RunPowerShellScript

$output.Value[0].Message

Output:

Output

You can also write a script which can run on the entire Azure Subscription, Resource Group or on few VMs. Below script will run on few VMs and stores the output on the respective VM text file.

$vms = @("TestVM2k19","TestVM2k16","TestVM2k12")

foreach($vm in $vms){
    $vmres = Get-AzVM -Name $vm
    Invoke-AzVMRunCommand `
        -VMName $vmres.Name `
        -ResourceGroupName $vmres.ResourceGroupName `
        -ScriptPath C:\Temp\GetDisabledServices.ps1 `
        -CommandId RunPowerShellScript | Out-File "C:\Temp\$($Vm).txt"
}

PS: The above script is just a sample script to run on multiple VMs. You can add error handling part, make script to execute parallel or more as per your need.