Jan 23 2018
01:17 PM
- last edited on
Apr 07 2022
04:52 PM
by
TechCommunityAP
Jan 23 2018
01:17 PM
- last edited on
Apr 07 2022
04:52 PM
by
TechCommunityAP
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.
Jan 23 2018 11:05 PM
SolutionHi, 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.
Aug 01 2018 11:09 AM
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.
Aug 01 2018 10:57 PM
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:
https://github.com/krnese/arm-sample/blob/master/mgmt/policies/mgmt/omsDeployIfNotExistsWindows.json
https://github.com/krnese/arm-sample/blob/master/mgmt/policies/mgmt/omsDeployIfNotExistsLinux.json
Sep 11 2018 11:20 AM
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.
Jan 23 2018 11:05 PM
SolutionHi, 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.