Script to remove all rules from collection

' Setup a connection to the local provider.

Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")

Set swbemServices= swbemLocator.ConnectServer(".", "root\sms")

Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")


For Each Location In providerLoc

    If location.ProviderForLocalSite = True Then

        Set swbemServices = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)

        Exit For

    End If



Set swbemContext = CreateObject("WbemScripting.SWbemNamedValueSet")

swbemContext.Add "SessionHandle", swbemServices.ExecMethod("SMS_SiteControlFile", "GetSessionHandle").SessionHandle


Call DeleteCollectionRules(swbemServices, "BLB0000D")


Sub DeleteCollectionRules(connection, existingCollectionID)


    Dim instCollection

    Dim RuleSet

    Dim Rule


    ' Get a collection using the existingCollectionID variable passed in.   

    Set instCollection = connection.Get("SMS_Collection.CollectionID='" & existingCollectionID & "'")


    'Get the array of embedded SMS_CollectionRule objects.

    RuleSet = instCollection.CollectionRules



    'Delete all membership rules in the collection.

    For Each Rule In RuleSet

        instCollection.DeleteMembershipRule Rule



    'Refresh the collection members to reflect any deletions.


    'The False parameter value tells the method not to refresh subcollections.

    instCollection.RequestRefresh False


End Sub

Comments (0)

Skip to main content