Operations Management Suite agent deployment with Powershell for Azure VMs

Azure provides easy way to deploy Operations Management Suite (OMS) agents on Azure VMs. All you need to do is to navigate to Log Analytics blade, choose Virtual Machines from Data Sources and select which VMs you want to add to OMS workspace by clicking Connect on the VM.


But what if you have hundreds or even thousands of VMs running on Azure? This is not going to be an easy task to click on each VM and connect to OMS workspace. Powershell is the answer! With the code below you will be in a good shape to deploy OMS extensions to all VMs on all Resource Groups in your subscription.


Select-AzureRmSubscription -SubscriptionId $SubID
$Settings = @{"workspaceId"="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"};
$ProtectedSettings = @{"workspaceKey"="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"};
$RG = Get-AzureRmResourceGroup

foreach ($Resource in $RG.ResourceGroupName)
 $VMs = Get-AzureRmVM -ResourceGroupName $Resource
 ForEach ($VM in $VMs)
  Set-AzureRmVMExtension -ExtensionType MicrosoftMonitoringAgent -Name MicrosoftMonitoringAgent -Publisher Microsoft.EnterpriseCloud.Monitoring `
        -ResourceGroupName $Resource -TypeHandlerVersion 1.0 -VMName $VM.Name -Location $VM.Location -ProtectedSettings $ProtectedSettings -Settings $Settings


Leave a Reply

Your email address will not be published.