ConfigMgr 2012–Create Static 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 static 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 static collection:
Name, Comment, OwnedByThisSite, CollectionType, LimitToCollectionID
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: CreateStaticCollections.vbs
'//
'// Purpose: Used to Create Static Collections
'//
'// Usage: cscript CreateStaticCollections.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
Next
'// Create Static Device Collections
Call CreateStaticCollection(swbemconnection, "New Static Device collection", "New Static Device collection Comment", true, 2, "SMS00001")
'// Create Static User Collections
Call CreateStaticCollection(swbemconnection, "New Static User collection", "New Static User collection Comment", true, 1, "SMS00004")
Sub CreateStaticCollection(swbemconnection, newCollectionName, newCollectionComment, ownedByThisSite, 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_
'// Get the collection.
Set newCollection = swbemconnection.Get(collectionPath.RelPath)
'// Call RequestRefresh to initiate the collection evaluator.
newCollection.RequestRefresh False
End Sub