We wanted to deploy software to our environment via and assigned advertisement in SCCM, but we wanted to be able to install packages to a subset of a collection. If there is an issue the next day, the whole enterprise would not down. We already had a collection that identified machines that need the package, we just want to deploy to the first 15 one day, and another 15 the next day.
Since WQL does not allow a SQL “TOP” I did not think I would be able to do it via a complex query. So I wrote the following vbscript to find machines in one Collection and add them to another collection:
Sub CopyMachinesToCollection (SOURCE_COLLECTION,TARGET_COLLECTION,ResourcesAtATime) Set objLocation = CreateObject("WbemScripting.SWbemLocator") Set oService = objLocation.ConnectServer("server", "root\SMS\site_XXX") Set oSourceCollectionMembers = oService.ExecQuery("SELECT ResourceID, Name FROM SMS_FullCollectionMembership WHERE CollectionID = '" & SOURCE_COLLECTION & "'") Set oTargetCollection = oService.Get("SMS_Collection.CollectionID='" & TARGET_COLLECTION & "'") ' Add ResourcesAtATime resources to counter=0 For Each Resource In oSourceCollectionMembers if counter < ResourcesAtATime then 'Wscript.Echo Resource.ResourceID & "-" & Resource.Name Set DirectRule = oService.Get("SMS_CollectionRuleDirect").SpawnInstance_() DirectRule.ResourceClassName = "SMS_R_System" DirectRule.ResourceID = Resource.ResourceID DirectRule.RuleName = Resource.Name oTargetCollection.AddMembershipRule DirectRule, SMSContext oTargetCollection.RequestRefresh False end if counter=counter+1 Next End Sub Sub DeleteTargetCollection (TARGET_COLLECTION) Set objLocation = CreateObject("WbemScripting.SWbemLocator") Set oService = objLocation.ConnectServer("svnyem01", "root\SMS\site_SVC") Set oTargetCollection = oService.Get("SMS_Collection.CollectionID='" & TARGET_COLLECTION & "'") ' Delete all in oTargetCollection If Not IsNull(oTargetCollection.CollectionRules) Then For Each Rule In oTargetCollection.CollectionRules wscript.echo Rule.RuleName oTargetCollection.DeleteMembershipRule Rule Next oTargetCollection.RequestRefresh False End If End Sub
Second sub removes all machines from the collection, and the first copies the first “x” from the soure to the destination