OMS Agent Removal

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 $

    Write-Host “Working on Subscription“ $

    #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 $ -Name 'MicrosoftMonitoringAgent' -Force
                    remove-AzureRmVMExtension -ResourceGroupName $RG.resourcegroupname -VMName $ -Name 'OmsAgentForLinux' -Force
                else {
                    write-host "Log Name is Not Matching default name"

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){