I wanted to add a bunch of VMs in a subscription to a log analytics workspace in Azure so I can add it to update management etc. Didn’t find any command when I Googled for this, so here’s a blog post in case anyone else searches for it.
There isn’t anything fancy in what I am doing. The way to do it for a single VM is in this official doc. I just loop around it for all VMs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# all VMs in the subscription (which you set via Set-AzContext) $PublicSettings = @{ "workspaceId" = "" } $ProtectedSettings = @{ "workspaceKey" = "" } # Using -Status switch to get the status too Get-AzVM -Status | Where-Object{ $_.Powerstate -eq "VM running" } | ForEach-Object { $VMName = $_.Name $ResourceGroupName = $_.ResourceGroupName $Location = $_.Location Write-Host "Processing $VMName" Set-AzVMExtension -ExtensionName "MicrosoftMonitoringAgent" ` -ResourceGroupName "$ResourceGroupName" ` -VMName "$VMName" ` -Publisher "Microsoft.EnterpriseCloud.Monitoring" ` -ExtensionType "MicrosoftMonitoringAgent" ` -TypeHandlerVersion 1.0 ` -Settings $PublicSettings ` -ProtectedSettings $ProtectedSettings ` -Location "$Location" } |
Get the workspace ID and key from Log Analytics workspace > [your workspace] > Overview > “1. Connect a data source” > “Windows, Linux, and other sources”