OMS Agent Removal

Welcome Forums General PowerShell Q&A OMS Agent Removal

Viewing 1 reply thread
  • 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: 115
      Points: 433
      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){
      
      }
Viewing 1 reply thread
  • The topic ‘OMS Agent Removal’ is closed to new replies.