| | 0

Microsoft Configuration Manager 2012 and Powershell–Part 2

Just wanted to keep you up-to-date regarding my task to automatically build a folder-collection structure in a customer’s Configuration Manager 2012 environment. (see also article https://www.sepago.de/e/david/2012/02/24/create-folders-in-microsoft-system-center-configuration-manager-with-powershell )

Create Configuration Manager device collections

We already know how to create folders underneath the collections node. Now we need to create the collections our clients will one day be members of.
This isn’t too hard a task, so here’s my script:

#Functionality: creates a ConfigMgr collection
#Author: David O'Brien
#date: 25.02.202
Function Create-Collection($CollectionName)
$CollectionArgs = @{
Name = $CollectionName;
CollectionType = "2"; # 2 means Collection_Device, means Collection_User
LimitToCollectionID = "SMS0000"

Set-WmiInstance -<span>Class</span> SMS_Collection -arguments $CollectionArgs -namespace <span>"root\SMS\Site_$sitename"</span> | Out-Null


$CollectionName = Get-Random # creates a random number <span>for</span> testing
$sitename = "PRI"

Create-Collection $CollectionName

You all know that we’ve got “Limiting collections” now in Configuration Manager 2012, so we need to provide our script with the limiting collection’s ID. In this example it’s the “All Systems” device collection.

Move Configuration Manager device collections

The thing with collections and folders is a bit tricky. While creating a collection it always gets created in the collection node’s root. This is why we can’t provide our target folder during collection creation.
It’s a two-step process!

After creation we can now move our collection away from the root into a known folder.
Either hardcode the “ObjectContainerNodeID” of your target folder into the script, or better, evaluate it during script execution depending on your collection and folder name.
The latter will be very easy when you stick to a naming convention like
$Folder name = %abbreviation for remote site% like “CGN” for Cologne
$Collection Name = $Folder name_Clients…

Just go ahead and split your collection name until it matches your folder name and go ahead like this:

#Functionality: moves a ConfigMgr collection from one folder to an other
#Author: David O'Brien
#date: 25.02.2012
function move-Collection($SourceContainerNodeID,$collID,$TargetContainerNodeID)
$Computer = "."
$Class = "SMS_ObjectContainerItem"
$Method = "MoveMembers"
$MC = [WmiClass]"\\$Computer\ROOT\SMS\site_$sitename:$Class"
$InParams = $mc.psbase.GetMethodParameters($Method)
$InParams.ContainerNodeID = $SourceContainerNodeID #usually 0 when newly created Collection
$InParams.InstanceKeys = $collID
$InParams.ObjectType = "5000" #5000 <span>for</span> Collection_Device, 5001 for Collection_User
$InParams.TargetContainerNodeID = $TargetContainerNodeID #needs to be evaluated

"Calling SMS_ObjectContainerItem. : MoveMembers with Parameters :"
$inparams.PSBase.properties | select name,Value | format-Table
$R = $mc.PSBase.InvokeMethod($Method, $inParams, $Null)
"Result :"

$R | Format-list

Now you’ve got two independent functions and need to combine them into one script.

Any questions? Just comment here, send me a mail or contact me on twitter (@david_obrien)