OMS Agent Removal

Welcome Forums General PowerShell Q&A OMS Agent Removal

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #172249

    Participant
    Topics: 1
    Replies: 0
    Points: 12
    Rank: Member

    Hello Folks,

    i Have a request to remove Azure MMA Agent(linux and win) from Azure vms as long as the Workspace is similar to default*: i am able to remove for all workspaces with the below script: but i cant seem to target just the default workspace : i must be missing something small here

    $subscriptions = Get-AzureRMSubscription
    
    #loop subs
    ForEach ($sub in $subscriptions) {
    
        select-azureRMsubscription -subscriptionName $sub.name
    
        Write-Host “Working on Subscription“ $sub.name
    
        #loop rgs
        $RGs = Get-AzureRMResourceGroup
        foreach ($RG in $RGs) {
            $VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName
            foreach ($VM in $VMs) {
                $OSType = $VM.StorageProfile.OsDisk.OsType
                $wksps = (Get-AzureRmOperationalInsightsWorkspace)
                foreach ($wksp in $wksps -and $wksp.Name -like "default*") {
                    if ($OSType -eq "windows" -or $OSType -eq "Linux") {
                        remove-AzureRmVMExtension -ResourceGroupName $RG.resourcegroupname -VMName $VM.name -Name 'MicrosoftMonitoringAgent' -Force
                        remove-AzureRmVMExtension -ResourceGroupName $RG.resourcegroupname -VMName $VM.name -Name 'OmsAgentForLinux' -Force
                    }
                    else {
                        write-host "Log Name is Not Matching default name"
                    }
                }
            }
        }
    }
    
    
  • #172411

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Is there a specific reason you combined a filter in the Foreach loop? Have you tried removing -and $wksp.Name -like "Default*" and confirm if "Default" shows up in the $wksp variable? I would almost recommend this alternative. The 1 is used to ignore case sensitivity.

    ForEach ($wksp.Name.StartsWith('default',1) in $wksps){
    
    }

You must be logged in to reply to this topic.