Jump to content


andymc

VBscript - problem with adding membershiprule to collection

Recommended Posts

Hello at all,

 

I’m working on a script which creates a SCCM collection and appends a collection rule. The function createCollection creates and moves the collection without any problems.

The collection rule object is also created without any error codes, but at adding the collection rule (newCollection.AddMembershipRule newCollectionRule) I get a “general error”.

 

 

Has anybody an idea how to fix this problem?

 

Function createCollection(collectionName)

    Dim validQuery, queryExpr

' creates the collection.
    Set newCollection = objSccm.Get("SMS_Collection").SpawnInstance_()
    newCollection.Name = collectionName
    newCollection.OwnedByThisSite = True
    newCollection.Comment = "Comment"
         
    Set collectionPath = newCollection.Put_

' Attempts to obtain the collection ID of the newly created collection.
Set Collection = objSccm.ExecQuery ("select * from SMS_Collection where Name='" & collectionName & "'")
For each objCollection in Collection
    strCollectionID=objCollection.CollectionID
Next
If
 strCollectionID = "" Then
    Wscript.Echo "Unable to obtian a collection ID for the newly created collection."
    WScript.Quit
Else
    ' Attempts to move the newly created collection into the desired parent collection.
    Set newCollectionRelation = objSccm.Get("SMS_CollectToSubCollect").SpawnInstance_()
    newCollectionRelation.parentCollectionID = strParentCollectionID
    newCollectionRelation.subCollectionID = strCollectionID
    newCollectionRelation.Put_
End If 

    'Attempts to create and add the query rule group to the collection!
    Set stdQuery = objSccm.Get("SMS_CollectionRuleQuery")
    validQuery = stdQuery.ValidateQuery(queryExpr)
    
    If validQuery Then
    
    Set newCollectionRule = stdQuery.SpawnInstance_
    newCollectionRule.QueryExpression = queryExpr
    newCollectionRule.RuleName = "Rulename"
    
    Set newCollection = objSccm.Get(collectionPath.RelPath)
    newCollection.AddMembershipRule newCollectionRule
    newCollection.RequestRefresh False 
    
End If
End Function

 

Share this post


Link to post
Share on other sites

Problem is solved.The code is okay and working.

The script tried to create a query rule which is not limited to a collection (the same like a limitation to "All Systems"), but the executing account was not permitted to read values from "All Systems" collection.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.