Introduction

This is the 10th instalment of the Automating OpsMgr series. Previously on this series:

As I have previously demonstrated how to create and update OpsMgr groups using the OpsMgrExtended module, it’s now the time cover how to delete groups in OpsMgr.

Deleting groups that are defined in unsealed management packs can be easily accomplished using the Remove-OMGroup function from the OpsMgrExtended module. This function deletes the group class definition and discoveries from the unsealed MP. However, since it’s very common for OpsMgr administrators to also create dependency monitors for groups (for group members health rollup), you cannot simply use Remove-OMGroup function to delete groups when there are also monitors targeting this group. Therefore, I have written  a sample runbook to delete the group as well as monitors targeting the group (if there are any).

Runbook Delete-OpsMgrGroup

Workflow Delete-OpsMgrGroup
{
  Param(
  [Parameter(Mandatory=$true)][String]$GroupName,
  [Parameter(Mandatory=$true)][Boolean]$IncreaseMPVersion
  )
 
  #Get OpsMgrSDK connection object
  $OpsMgrSDKConn = Get-AutomationConnection -Name "OpsMgrSDK_HOME"

  #Firstly, make sure the monitors targeting this group is deleted (i.e dependency monitors for health rollup)
  Write-Verbose "Checking dependency monitors targeting the group '$GroupName'."
  $bDeleteMonitors = InlineScript {
    #Connect to MG
    $MG = Connect-OMManagementGroup -SDKConnection $USING:OpsMgrSDKConn
    $Group = $MG.GetMonitoringClasses($USING:GroupName)
    $GroupMP = $Group.GetManagementPack()
    If ($GroupMP.Sealed  -eq $true)
    {
      Write-Error "The group is defined in a sealed MP, unable to continue."
      Return $false
    }
    $GroupID = $Group.Id.ToString()
    $MonitorCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorCriteria("Target='$GroupID'")
    $Monitors = $MG.GetMonitors($MonitorCriteria)
    Foreach ($Monitor in $Monitors)
    {
      Write-Verbose "Deleting '$($Monitor.Name)'..."
      $MonitorMP = $Monitor.GetManagementPack()
      $Monitor.Status = "PendingDelete"
      Try {
        $MonitorMP.Verify()
        $MonitorMP.AcceptChanges()
      } Catch {
        Write-Error $_.Exception.InnerException.Message
        Return $false
      }
    }
    Return $true
  }
  If ($bDeleteMonitors -eq $true)
  {
    $bGroupDeleted =Remove-OMGroup -SDKConnection $OpsMgrSDKConn -GroupName $GroupName -IncreaseMPVersion $IncreaseMPVersion
  }
  If ($bGroupDeleted -eq $true)
  {
  Write-Output "Done."
  } else {
  throw "Unable to delete group '$GroupName'."
  exit
  }
}

In order to use this runbook, you will need to update Line 9, with the name of your SMA connection object.

SNAGHTML1591389

This runbook takes 2 parameters:

image

  • GroupName: the name of the group you are deleting. Please note you can only delete groups defined in unsealed MPs.

  • IncreaseMPVersion: Boolean variable, specify if the unsealed MP version should be increased by 0.0.0.1

Runbook Result:

image

Verbose Messages (deleting dependency monitors):

SNAGHTML14fa1a1

Conclusion

This post is rather short comparing to some of the previous ones in this series. I have few ideas for the next post, but haven’t decided which one am I going to write first. Anyways, until next time, happy automating!

Leave a comment