skip to Main Content

I have loads of migrated VMs which have got multiple Azure log analytic workspaces tagged/configured at VM level.
So what if I don’t want to delete the LAW IDs configured at VM level (not in Azure portal) by giving the definite list as I don’t have a pre-known list of those multiple LAW rather just keep needed one by matching it within list and delete rest. I don’t find an option to do so in documentation! Only looking for doing this via powershell script

Thanks in advance!

By using a powershell script on Azure Doc. we are able to delete multiple LAW Ids by enlisting them, but then there are so many Vms and tenants that it is not possible to keep doing it like this.

2

Answers


  1. Chosen as BEST ANSWER

    We figured it out and below gets the job done on Target VM without having to log on to VM and removing the Workspace configuration ID which is not needed and retaining the one which matches, at VM level not at Azure portal level.

    ########### ADD/REMOVE LAW ID #############
        $MMA = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
        $WorkspaceData = $MMA.GetCloudWorkspaces() 
        $RequiredID = $WorkspaceData | where workspaceId -EQ "workspaceIdToKeep"
        $CheckID = [String]::IsNullOrWhiteSpace($RequiredID)
        if($CheckID -eq $True){
            $MMA.AddCloudWorkspace("AddYourCloudWorkspaceID")
        }
    
        $AllWorkspaceData = $MMA.GetCloudWorkspaces()
        $WorkspaceIDList = $AllWorkspaceData | select -ExpandProperty workspaceId
        foreach($WorkspaceID in $WorkspaceIDList){
            if($WorkspaceID -ne "workspaceIdToKeep"){
                $MMA.RemoveCloudWorkspace($WorkspaceID)
                #Start-Sleep -s 2
            }
        }
        $MMA.ReloadConfiguration()
    

    Relevant image - https://i.stack.imgur.com/Zk4jq.png


  2. After reproducing from my end, I could able to achieve your requirement using the below script.

    $AllLaws=Get-AzureRmOperationalInsightsWorkspace
    $Requiredlaws = "<Your_Required_LAW_1>","<Your_Required_LAW_2>"...
    foreach($law in $AllLaws)
    {
        if($law.Name -NotIn $Requiredlaws)
        {
            Remove-AzOperationalInsightsWorkspace -ResourceGroupName $law.ResourceGroupName -Name $law.Name
        }
    }
    

    RESULTS:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search