ConfigMgr 2012–Create Dynamic Collection Script

For those of you that might be interested in automating some of your collections for your ConfigMgr 2012 Hydration or test labs the following should help guide you on how to create dynamic configmgr collections. There have been a few changes with collections in ConfigMgr 2012 which make the process of scripting collections slightly different.

First there are no more subcollections so we no longer have to supply the ParentCollection Property.

Secondly collections are split up into two different groups which cannot be mixed together, Device and user groups. There is a property to specify which type of collection you want to create called CollectionType that accepts the following parameters: “1 = User, 2 = Device”.

Lastly every collection has to be limited to another collection regardless of the type of collection being created. If you are creating a device collection then you need to limit that collection to another device collection and the same applies to user based collections they need to be limited to user based collections.

Below are the required parameters for creating a dynamic collection:

Name, Comment, OwnedByThisSite, CollectionType, LimitToCollectionID, queryForRule, ruleName

The following is an example script creating both a Device and User Collection.

Or you can download the script from the following Link.

Disclaimer: This code was created and tested on ConfigMgr 2012 (Beta) 2 and is subject to change.

‘// Script: CreateDynamicCollections.vbs
‘// Purpose: Used to Create Dynamic Collections
‘// Usage: cscript CreateDynamicCollections.vbs
‘// Version: 1.0 – 04 Sept 2011 – Brandon Linton
‘// Disclaimer: This script is provided "AS IS" with no warranties, confers no rights and
‘// is not supported by the authors or Inkbal Consulting, LLC.

‘ Setup a connection to the local provider.
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set swbemconnection= swbemLocator.ConnectServer(".", "root\sms")
Set providerLoc = swbemconnection.InstancesOf("SMS_ProviderLocation")

For Each Location In providerLoc
If location.ProviderForLocalSite = True Then
Set swbemconnection = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
Exit For
End If

‘// Create Dynamic Device Collections
Call CreateDynamicCollection(swbemconnection, "New Dynamic Device Collection", "New Dynamic Device Collection Comment", true, "SELECT * from SMS_R_System", "New Rule Name", 2, "SMS00001")

‘// Create Dynamic User Collections
Call CreateDynamicCollection(swbemconnection, "New Dynamic User Collection", "New Dynamic User Collection Comment", true, "SELECT * from SMS_R_User", "New Rule Name", 1, "SMS00004")

Sub CreateDynamicCollection(swbemconnection, newCollectionName, newCollectionComment, ownedByThisSite, queryForRule, ruleName, CollectionType, ExistingLimitToCollectionID)

‘// Create the collection.
Set newCollection = swbemconnection.Get("SMS_Collection").SpawnInstance_
newCollection.Name = newCollectionName
newCollection.Comment = newCollectionComment
newCollection.OwnedByThisSite = ownedByThisSite
newCollection.CollectionType = CollectionType
newCollection.LimitToCollectionID = ExistingLimitToCollectionID
‘// Save the new collection and save the collection path for later.
Set collectionPath = newCollection.Put_

‘// Create a new collection rule object for validation
Set queryRule = swbemconnection.Get("SMS_CollectionRuleQuery")

‘// Validate the query (good practice before adding it to the collection).
validQuery = queryRule.ValidateQuery(queryForRule)

‘// continue with processing, if the query is valid.
If validQuery Then

‘// Create the query rule.
Set newQueryRule = QueryRule.SpawnInstance_
newQueryRule.QueryExpression = queryForRule
newQueryRule.RuleName = ruleName

‘// Add the new query rule to a variable.
Set newCollectionRule = newQueryRule
‘// Get the collection.
Set newCollection = swbemconnection.Get(collectionPath.RelPath)
‘// Add the rules to the collection.
newCollection.AddMembershipRule newCollectionRule

‘// Call RequestRefresh to initiate the collection evaluator.
newCollection.RequestRefresh False
End If

End Sub


Written by , Posted .