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?

,

3 Responses to PowerShell to move a VM to a new Log Analytics WorkSpace

  1. Andre++ October 20, 2019 at 5:29 am #

    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 May 20, 2020 at 8:48 am #

    Thanks you very much. It helped me a lot.

  3. Simon June 9, 2021 at 6:12 pm #

    Simple, but helpful to reference your post. Thanks!

Leave a Reply

Powered by WordPress. Designed by WooThemes