PowerShell script to list all the extensions for a specific Azure ARM virtual machine
We have been looking for a script which will help us enumerate and list all the extensions for Azure ARM virtual machines and their properties, e.g. Name, Tags assigned, ProvisioningState, etc. and were almost giving up when this thought came upon to write a small one on our own. sharing here before things get lost.
#Login to Azure
Login-AzureRmAccount
#Set Parameters for Execution
$SubscriptionName = ‘<subscription Name>‘
#Select the Subscription you want to work.
Select-AzureRmSubscription -SubscriptionName $SubscriptionName
#Loop through all the Virtual Machines in the subscription
ForEach ($rmVM in Get-AzureRmVM)
{
#Check if the specific VM has extensions, if yes, proceed further.
If ($rmVM.Extensions.Count -gt 0)
{
#Get the Azure VM Object using Get-AzureRMVM commandlet – strange this seems to work otherwise returns blank value
$vms = Get-AzureRmVM -Name $rmVM.Name -ResourceGroupName $rmVM.ResourceGroupName
#loop through all the extensions via a for loop
For ($i=0; $i -lt $vms.Extensions.Count; $i++)
{
# Code snippet to collect the extension details and store into an array
Write-Host $vms.Name “- Extensions – ” $vms.Extensions[$i].Name ” – ” $vms.Extensions[$i].Tags ” – ” $vms.Extensions[$i].ProvisioningState
}
}
}