How to run PowerShell scripts against multiple Azure VMs in parallel by using Run Command

Written by 9:28 am Microsoft Azure, PowerShell • 16 Comments

How to run scripts against multiple Azure VMs by using Run Command

I wrote a blog post on how to run scripts in your Azure VM by using Run Command, and explained how handy this feature is to manage Azure virtual machines (VMs). In this blog post, we are going to have a look at how you can run scripts against multiple Azure virtual machines (VMs) by using PowerShell and the Invoke-AzVMRunCommand feature.

Usually, you can access your Azure virtual machine (VM) in multiple ways, like SSH or RDP. However, if you have issues with the RDP or SSH network configuration, or don’t have any network access at all, the Run Command feature is another option. Run Command can run a PowerShell or shell script within an Azure VM remotely by using the Azure Virtual Machine Agent. This scenario is especially useful when you need to run scripts against Azure VMs where you do not have network access.

You use Run Command for Azure VMs through the Azure portalREST APIAzure CLI, or PowerShell. Like I showed you in my blog post on Microsoft Tech Community.

Azure VM Run Command in the Azure Portal
Azure VM Run Command in the Azure Portal

Using Azure PowerShell

You can also use Azure PowerShell to use the run command capabilities to run PowerShell scripts against the guest agent inside the Azure VM. For that, you can simply use the Invoke-AzVMRunCommand cmdlet from the Az PowerShell module. You can also run this command directly from Azure Cloud Shell as well.

How to run PowerShell scripts against multiple Azure VMs by using Run Command in Parallel

Now here is how you can use PowerShell 7 and the Azure PowerShell module, to run scripts against multiple Azure VMs in parallel. For that, I am using a simple Foreach-Object to run the script in “script.ps1” against all my Azure VMs in a specific resource group. By default, this would take some time because it would run through all the virtual machines in sequential order. However, with PowerShell 7 we can use the -Parallel parameter to run the commands in parallel.

#Azure Subscription I want to use
$subscriptionId = "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
#Resource Group my VMs are in
$resourceGroup = "test-azurevms-rg"

#Select the right Azure subscription
Set-AzContext -Subscription $subscriptionId

#Get all Azure VMs which are in running state and are running Windows
$myAzureVMs = Get-AzVM -ResourceGroupName $resourceGroup -status | Where-Object {$_.PowerState -eq "VM running" -and $_.StorageProfile.OSDisk.OSType -eq "Windows"}

#Run the scirpt again all VMs in parallel
$myAzureVMs | ForEach-Object -Parallel {
    $out = Invoke-AzVMRunCommand `
        -ResourceGroupName $_.ResourceGroupName `
        -Name $_.Name  `
        -CommandId 'RunPowerShellScript' `
        -ScriptPath .\script.ps1 
    #Formating the Output with the VM name
    $output = $_.Name + " " + $out.Value[0].Message
    $output   
}
How to run PowerShell scripts against multiple Azure VMs in parallel by using Run Command
How to run PowerShell scripts against multiple Azure VMs in parallel by using Run Command

I also modified the output, so it shows the VM name I have run the script against, and I selected only the Message output of Invoke-AzVMRunCommand.

You can also check out my video on YouTube:

Conclusion

I hope this blog post helps you to run PowerShell scripts against multiple Azure virtual machines (VM) in parallel using the VM run command. If you have any questions feel free to leave a comment.

Tags: , , , , , , , , , , , Last modified: March 18, 2021
Close