Forum Discussion

Dante Nahuel Ciai's avatar
Dante Nahuel Ciai
Brass Contributor
Jan 23, 2018

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.

  • 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.

  • Hi Dante,

     

    you can use this.

     

    Add-AzureRmAccount

    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.

  • 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.

Resources