This example creates a collection object (SMS_Collection) and shows the collection-to-collection relationship and a direct membership rule. It also shows how the collection-to-collection and collection membership rules are associated with the collection.
This script adds the user that runs the script to the collection. This script can be used on a Web page to add the current user to a collection that has software advertised to it. Users can then initiate their own software distributions.
For more information about collections, see the following resources:
Dim objSWbemLocator Dim objSWbemServices Dim objSWbemContext Dim objSWbemInst Dim ProviderLoc Dim Location Dim NewName NewName = "Current User" 'Connect to provider namespace for local computer. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") Set objSWbemServices= objSWbemLocator.ConnectServer(".", "root\sms") Set ProviderLoc = objSWbemServices.InstancesOf("SMS_ProviderLocation") For Each Location In ProviderLoc If Location.ProviderForLocalSite = True Then Set objSWbemServices = objSWbemLocator.ConnectServer _ (Location.Machine, "root\sms\site_" + Location.SiteCode) End If Next 'Determine whether the specified Collection name is unique. alreadyused = False Set Collections = objSWbemServices.ExecQuery("Select * From SMS_Collection") For Each Collection In Collections If Collection.Name = NewName Then alreadyused = True Next If alreadyused Then MsgBox "This collection name is already in use. Please enter a different name." Else 'Create the collection. Dim newCollection Set newCollection = objSWbemServices.Get("SMS_Collection").SpawnInstance_() newCollection.Name = NewName newCollection.OwnedByThisSite = True newCollection.comment = "this is just a test" path=newCollection.Put_ 'Get the automatically assigned collection ID 'for the new collection. Set Collection=objSWbemServices.Get(path) newcollectionid= Collection.CollectionID 'Create the collection relationship. 'You could use the VerifyNoLoops method of the SMS_Collection class 'if you want to ensure that you won't create a loop of collections. Dim newCollectionRelation Set newCollectionRelation = objSWbemServices.Get( "SMS_CollectToSubCollect" ).SpawnInstance_() newCollectionRelation.parentCollectionID = "COLLROOT" newCollectionRelation.subCollectionID = newcollectionid newCollectionRelation.Put_ 'Determine the current user, as a resource ID. Set oWshNetwork=CreateObject("Wscript.Network") username = oWshNetwork.UserName Set Users = objSWbemServices.ExecQuery("Select * From SMS_R_User WHERE Name LIKE ""%" + username + "%""") For Each User In Users If User.username = username Then ResID = User.ResourceID wscript.echo username End If Next 'You might want to handle the contingency of a user who is new to 'the domain and hasn't been added to the list of SMS users yet. 'Create a direct collection rule for the user you just determined. Set CollectionRule = objSWbemServices.Get("SMS_CollectionRuleDirect").SpawnInstance_() CollectionRule.ResourceClassName = "SMS_R_User" CollectionRule.RuleName = "ResourceID=" & ResID CollectionRule.ResourceID = ResID 'Add the rule to the collection. Collection.AddMembershipRule CollectionRule If Err.Number = 0 Then Wscript.Echo "You were added to the " + Collection.Name + " collection!" End If 'This is the end of the test for a unique collection. End If
Compiling the Code
- Requires Windows 2000 Server SP2 or later.
- Requires an SMS 2003 Site Server.