PowerShell to move a VM to a new Log Analytics WorkSpace

This code uninstalls the Microsoft Monitoring agent and re-installs it to a new WorkSpace.


# change your VM Name and it's resource group
$vm = get-azurermvm -VMName YourVMName -ResourceGroupName VMResourceGroup
Remove-AzureRmVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name MicrosoftMonitoringAgent -Force
# put in your new workspaceId & workspaceKey
$workspaceId = "NewWorksSpaceID"
$workspaceKey = "SupaSecretKey"

$PublicSettings = @{"workspaceId" = $workspaceId;"stopOnMultipleConnections" = $false}
$ProtectedSettings = @{"workspaceKey" = $workspaceKey}

Set-AzureRmVMExtension -ExtensionName "MicrosoftMonitoringAgent" -ResourceGroupName $vm.resourcegroupname -VMName $vm.name `
-Publisher "Microsoft.EnterpriseCloud.Monitoring" `
-ExtensionType "MicrosoftMonitoringAgent" `
-TypeHandlerVersion 1.0 `
-Settings $PublicSettings `
-ProtectedSettings $ProtectedSettings `
-Location $vm.Location

Nothing special, just thought I would put it here. Mayby it will help someone?


Posted

in

,

by

Tags:

Comments

3 responses to “PowerShell to move a VM to a new Log Analytics WorkSpace”

  1. Andre++ Avatar
    Andre++

    Needed a quick fix as via the portal I could not get it done. This helped me in the right direction.

    But above only works if your current extensionname is MicrosoftMonitoringtAgent, in my case it was MMAExtension.
    So better to do get-AzureRmVMEXtension first (and move the whole thing to the new AZ module)

  2. Keerthana Avatar
    Keerthana

    Thanks you very much. It helped me a lot.

  3. Simon Avatar
    Simon

    Simple, but helpful to reference your post. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *