Removing SCOM workflows via PowerShell and SDK
This is just a short post about how to remove SCOM workflows (Rules,Monitors and Overrides) via PowerShell. As you may have noticed, our OpsMgr PowerShell module provides several Cmdlets for GETting workflows, but not for removing them:
So, how do we remove a workflow? If you know the correct methods and how to use them, you can remove a workflow in three simple steps:
Step 1: Retrieving the workflow instance to be removed
This is usually done by using a method from the Management Group object and the GUID of the workflow:
PS> $Monitor = $ManagementGroup.GetMonitor(“9d029a5b-da8b-6090-4cb9-e442b5c13f2c”)
Step 2: Setting the status of the instance to “PendingDelete”
The instance provides a Status property:
The status is an enumeration of type Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus (see MSDN for details). To delete an instance the status has to be set to PendingDelete:
PS> $Monitor.status = $PendingDeleteState
Step 3: Accepting the change (storing the change in the ManagementPack)
Setting the status property is not enough. We have to explicitly save this change in the ManagementPack:
PS>$Monitor.GetManagementPack().AcceptChanges()
One thing remains: How do I get the $PendingDeleteState variable?
PS> $PendingDeleteState = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingDelete
To sum it up, these are the complete steps to remove a specific SCOM Monitor within the SCOM ManagementShell:
PS> $ManagementGroup = get-scommanagementgroup
PS> $Monitor = $ManagementGroup.GetMonitor(“9d029a5b-da8b-6090-4cb9-e442b5c13f2c”)
PS> $PendingDeleteState = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementStatus]::PendingDelete
PS> $Monitor.status = $PendingDeleteState
PS> $Monitor.GetManagementPack().AcceptChanges()
Remove-SCOMworkflow
To showcase this functionality I wrote a short universal function remove-scomworkflow, which you could download at TechNet Gallery here. It provides this functionality:
- Can remove one or multiple workflows of the same type in one step by providing an array of GUIDs
- Can remove Rules, Monitors and Overrides
- Uses PowerShell default Confirm mechanisms to request for confirmation
- Returns a custom object for each workflow which you can use for reporting or further processing