DeleteMemberShipRule CollectionRuleQuery in C#

Hello,
recently i'm creating a script that automatically removes a  single membership rule out of a SCCM collection using c#. For a directrule I've already managed to create the code and it works fine.
The problem is that in our compagny, we use query rule memberships instead of direct rules. for so far I'm not able to delete a single membership rule based on a query.
After executing the code, SCCM smsprov.log returns collectionrule not found. Seems to be that my queryexpression is not right.
Who can hep solving this issue, I'm allmost convinced it is something small.
Below my code snippet within a try /catch :
   string queryExpression = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.Name
in (\""+ system + "\")";
   IResultObject myCollection = connection.GetInstance(@"SMS_Collection.CollectionID='" + collID + "'");
   //Create a new rule object and save the resource ID
   IResultObject collectionRule = connection.CreateEmbeddedObjectInstance("SMS_CollectionRuleQuery");
   // fill in resourceID
   collectionRule["RuleName"].StringValue = system + " ResourceID = " + sysID;
   collectionRule["limitToCollectionID"].StringValue = collID;
   collectionRule["QueryExpression"].StringValue = queryExpression;
   //The rule object as parameter
   Dictionary<string, object> inParams = new Dictionary<string, object>();
   inParams.Add("collectionRule", collectionRule);
   // Delete the rule.
   myCollection.ExecuteMethod("DeleteMemberShipRule", inParams);
Thanks in advance

I am deleting using QueryID and it works. Hopefully, you find this useful:
// get your collectionID and queryID
IResultObject collection = wqlConnection.CreateInstance("SMS_Collection");
collection["CollectionID"].StringValue = collectionID;
collection.Get();
IResultObject query = wqlConnection.CreateInstance("SMS_CollectionRuleQuery");
query["QueryID"].StringValue = queryID;
Dictionary<string, object> membershipRuleParameters = new Dictionary<string, object>();
membershipRuleParameters.Add("CollectionRule", query);
collection.ExecuteMethod("DeleteMembershipRule", membershipRuleParameters);

Similar Messages

  • Remove direct computer association from collection by script

    Currently i have a OSD task sequence that is advertised to a specific collection. The aim of this task sequence reimages machines that are currently on the network, that for some reason or another has a problem which is easier solved by reimaging.
    At the moment i simply create a direct computer association in the collection and then the machine receives the job and it all works fine.
    I'd like to be able to remove the direct computer association from this collection after the job has finished automatically. Is this possible by adding in an additional task sequence to run a script?Matt Summer

    I used the code below and changed the specifics to my environment.  I put in my SMS server, site code, collection ID, my username/pass and also identified a specific computer name for testing purposes.  The script runs successfully without any
    errors but the object doesn't get deleted.  I can't figure out where its going wrong.  I updated collection membership and refreshed everything, but no dice, the computer object is still there.  I don't know if it makes a difference, but the
    object I am trying to delete was manually created by me for testing purposes.  
    I am running the script manually from the SCCM server using cscript, this is NOT part of a task sequence.  I just want people to be able to remove SCCM objects via script (and eventually a front end) instead of going into the console.
    You could add a step at the very end of the task sequence running a vb-script that removes the resource from the collection, something like the following:
    On Error Resume Next  
    Dim oNetwork, oLocator, oSWbemServices, oCollection  
    Dim sComputerName, sSMSServer, sSMSSiteCode, sCollectionID, RuleSet  
    Set oNetwork = CreateObject("WScript.NetWork")   
    sComputerName = oNetwork.ComputerName  
    sSMSServer = "SCCM" 
    sSMSSiteCode = "CEN" 
    sCollectionID = "CEN00001" 
    sUserName = "Domain\Username" 
    sUserPassword = "Password" 
    Set oLocator = CreateObject("WbemScripting.SWbemLocator")  
    oLocator.Security_.AuthenticationLevel = 6 
    Set oSWbemServices = oLocator.ConnectServer(sSMSServer, "root\sms\site_" & sSMSSiteCode, sUserName, sUserPassword)  
    Set oCollection = oSWbemServices.Get("SMS_Collection='" & sCollectionID & "'")
    RuleSet = oCollection.CollectionRules  
    For Each Rule In RuleSet  
        If Rule.Path_.Class = "SMS_CollectionRuleDirect" Then  
            If LCase(Trim(Rule.RuleName)) = LCase(Trim(sComputerName)) Then  
                oCollection.DeleteMembershipRule Rule  
            End If  
        End If  
    Next  
    WScript.Quit(0) 
    Just make sure the user you use has the apropriate rights in SCCM (rights on collection + connect rights), you probably should make it a service account that can't log on and strip it of every other right it doesn't need as the password is clear text and somone
    could possibly snatch it.

Maybe you are looking for

  • Installing from an older version

    Can I get some help with installing CS6 from CS3? I uninstalled CS3 after installing CS6 and it says files are missing do I have to reinstall CS3 for CS6 to work properly?

  • Ipad Air to Epson projector partial image projected

    When I connect my ipad air to an Epson XGA projector via the Lightning to vga adapter MD825ZM/A, the left hand 10% approx of the picture does not get projected - I have to drag the image to the right to see it. Does anyone know a fix please?

  • Please help me with this

    please help me with this  i brought sony z3 dual last month since i brought i noticed that when typing there is problem with screen touch system ,as there is specific letters not typing correctly  first i thought its from screen sticker came with the

  • Mac address table corruption?

    We are running Cisco 4500 chassis at the access layer, and have been for a few years without issue. Recently we started to experience issues where a mac address will just randomly "jump" to another port. User will call us and say their computer is no

  • Question abput Extending Classes

    Hi, Would anyone know if the following is valid?? class A extends B{} class B extends A{} Thanks in advance...