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.AddMembershipRulenewCollectionRule) I get a “general error”.

 

 

Has anybody an idea how to fix this problem?

 

FunctioncreateCollection(collectionName)

    DimvalidQuery, queryExpr

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

'AttemptstoobtainthecollectionIDofthenewlycreatedcollection.
SetCollection=objSccm.ExecQuery("select*fromSMS_CollectionwhereName='"&collectionName&"'")
ForeachobjCollectioninCollection
    strCollectionID=objCollection.CollectionID
Next
If
strCollectionID=""Then
    Wscript.Echo"UnabletoobtianacollectionIDforthenewlycreatedcollection."
    WScript.Quit
Else
    'Attemptstomovethenewlycreatedcollectionintothedesiredparentcollection.
    SetnewCollectionRelation=objSccm.Get("SMS_CollectToSubCollect").SpawnInstance_()
    newCollectionRelation.parentCollectionID=strParentCollectionID
    newCollectionRelation.subCollectionID=strCollectionID
    newCollectionRelation.Put_
EndIf

    'Attemptstocreateandaddthequeryrulegrouptothecollection!
    SetstdQuery=objSccm.Get("SMS_CollectionRuleQuery")
    validQuery=stdQuery.ValidateQuery(queryExpr)
    
    IfvalidQueryThen
    
    SetnewCollectionRule=stdQuery.SpawnInstance_
    newCollectionRule.QueryExpression=queryExpr
    newCollectionRule.RuleName="Rulename"
    
    SetnewCollection=objSccm.Get(collectionPath.RelPath)
    newCollection.AddMembershipRulenewCollectionRule
    newCollection.RequestRefreshFalse
    
EndIf
EndFunction

 

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.