This can be incorporated into the template that deploys the VM itself, too.
@description('The name of one or more VMs to be joined to the domain. If you specify more than one VM, separate their names with commas. E.g., vm1,vm2,vm3')
@minLength(1)
param targetVMs string
@description('Optional. The Azure location of the VM(s). E.g., australiaeast')
param location string = resourceGroup().location
@description('Workspace ID of the log analytics workspace to which these VMs are to be onboarded.')
param logAWId string
@description('Workspace key of the log analytics workspace to which these VMs are to be onboarded.')
@secure()
param logAWKey string
var vmArray = split(targetVMs, ',')
resource logawOnboard 'Microsoft.Compute/virtualMachines/extensions@2015-06-15' = [for item in vmListArray: {
name: '${trim(item)}/MSMonitoringAgent'
location: location
properties: {
publisher: 'Microsoft.EnterpriseCloud.Monitoring'
type: 'MicrosoftMonitoringAgent'
typeHandlerVersion: '1.0'
autoUpgradeMinorVersion: true
settings: {
workspaceId: logAWId
}
protectedSettings: {
workspaceKey: logAWKey
}
}
}]