Connect OMS on multiple VMs


Hi all

I'm managing a subscription with 400 VMs.
I need to connect them all to one OMS workspace.


Is there any way to perform this by Powershell without manually connecting one by one in the portal?

Thanks in advance.

4 Replies
best response confirmed by Stanislav Zhelyazkov (MVP)

Hi, Yes it is possible. The command will depend on the OS - Linux or windows but in general here is the code for Windows:


[string]$Settings          ='{"workspaceId":"' + $OMSLogAnalyticsWorkspaceID + '"}';
	                [string]$ProtectedSettings ='{"workspaceKey":"' + $OMSLogAnalyticsPrimaryKey + '"}';

Set-AzureRmVMExtension `
                                                -ResourceGroupName $ResourceGroupName `
                                                -VMName $VMName `
                                                -Name 'Microsoft.EnterpriseCloud.Monitoring' `
                                                -Publisher 'Microsoft.EnterpriseCloud.Monitoring' `
                                                -TypeHandlerVersion '1.0' `
                                                -ExtensionType 'MicrosoftMonitoringAgent' `
                                                -Location $Location `
                                                -SettingString $Settings `
                                                -ProtectedSettingString $ProtectedSettings 

For Linux is similar, only extension type is different OmsAgentForLinux and the name can be changed as well.

I think this script is only working for a single Resource Group that contains the 400 VMs.

what happens if we having all the VMs in different-different resource groups and regions.

what we do at that time how to move them all into the OMS. 

Hi Prashant,

I've only provided a snippet not a script. The snippet can be taken and you can implement whatever logic you want to walk trough every VM. The resource group, vm name and the location are all values that can be taken from the VM properties. Additionally now you can use Azure Policy to the agent installation was well.

Examples from Kristian Nese:


Hi Dante,


you can use this.



Set-AzureRmContext -Subscriptionid <Your Sub ID>

$workspaceName = "<Your OMS workspace name>"

$resourcegroup = "<Your OMS’s Resource Group>"


$workspaceId = "<Workspace ID>"

$workspaceKey = "<Key>"

$location = "<OMS workspace location>"


$vms = Get-content -Path "C:\temp\<file name>.txt"

$vmresourcegroup = "<VMs’ Resource Group>"


foreach ($vm in $vms) { Set-AzureRmVMExtension -ResourceGroupName $vmresourcegroup -VMName $vm -Name "MicrosoftMonitoringAgent" -Location $location -Publisher "Microsoft.EnterpriseCloud.Monitoring" -ExtensionType "MicrosoftMonitoringAgent" -TypeHandlerVersion "1.0" -SettingString "{'workspaceId': '$workspaceId'}" -ProtectedSettingString "{'workspaceKey': '$workspaceKey'}" } 


put the all VM's name into an TXT or an CSV file the provide the location and then run it into the powershell.


i think this will helpfull.