This post picks up where previous one left off, and looks at deleting SCOM connectors in code.
Since Microsoft System Center Operations Manager 2007 and 2012 allow administrators to view and edit properties of subscriptions and connectors, it is possible that an unsuspecting administrator “tweaks” your subscription a little bit, but just enough to break some design or coding assumptions in your application. For this reason, you may need, from time to time, to delete and recreate your connector and subscriptions automatically.
The good news is that deleting is much more simple than creating.
For VB.NET project requirements (such as references to SCOM 2012 SDK and namespace import), please refer to the article cited at the top of the post.
Delete All Subscriptions, then Connector
The code looks pretty simple:
Dim mg As ManagementGroup Dim conGuid As Guid Try mg = New ManagementGroup(My.Settings.ManagementGroup) cfa = mg.GetConnectorFrameworkAdministration conGuid = My.Settings.ConnectorGUID Try con = cfa.GetMonitoringConnector(conGuid) Dim subs = cfa.GetConnectorSubscriptions For Each s As MonitoringConnectorSubscription In subs If s.DisplayName = My.Settings.SubscriptionDisplayName Then cfa.DeleteConnectorSubscription(s) Next con.Uninitialize() cfa.Cleanup(con) Throw New ObjectNotFoundException Catch exx As ObjectNotFoundException 'handle new connector/subscription creation here End Try Catch ex As Exception 'something really bad happened - handle here End Try
Line 17 throws a bogus exception, of a specific type, that we are catching in the closest Catch block. This isn’t entirely necessary – you can eliminate that altogether and instead call your function that deals with creation of the new set of connector/subscription.
Lines 11 and 13 are key. We have to get a list of all subscriptions associated with the connector, and delete them before attempting to delete the connector itself. In this code sample, I am checking for subscription name, to make sure that I am not deleting anything that a customer may have created manually – but the point is, if they did, this code will fail to delete the connector if other unknown (to me) subscriptions exist.
I try to keep it simple for the customer, and create all connectors and subscriptions automatically. And also, recreate them when necessary, also automatically.