SCCM 2012: Create Collections and AD Groups – Jocha

After setting up a new domain and deploying SCCM 2012 I came to realize how much work it actually is to set up new applications. I have made a script which creates AD groups and Collections. I still create the Applications manually because they usually differ when it comes to how they need to be set up. Some products can be imported as Applications (MSI etc) while some (Autodesk products in particular) need to be set up as Packages.

Below you will find the script I made. Please note that it needs to be run from the SCCM server, or via remote Powershell/PSexec.
Also note that I wrote this script for application deployments towards Users, let me know if you need to Device-version.

#	Create Collections 1.1 - heineborn.com 2012-11-16
#
#       - EXECUTE -
#               Run the script with the following syntax:
#              .\Script.ps1 APP_Name_Version "This is my Application"
#
#	- FUNCTION -
#		Will create AD Groups and SCCM Collections
#
#	- PRE-REQS -
#               User needs administrative/modify privileges in SCCM and AD.
#		Needs to be executed from SCCM server.
#

Import-Module ActiveDirectory

$CollectionName=$args[0]
$Description=$args[1]
cls
If (!$CollectionName) { $CollectionName = Read-Host "What should the Collection be called? (ex. APP_VideoLAN_VLC_3)" }
If (!$Description) { $Description = Read-Host "Describe the Collection. (ex. VLC Media Player)" }

$Sitename = "PRI" #Change this: SCCM site name
$Domain = "HEINEBORN" #Change this: Domain
$DC = "DC.HEINEBORN.LOCAL" #Change this: Domain controller
$GroupOU = "OU=Applications,OU=Groups,DC=HEINEBORN,DC=LOCAL" #Change this: OU to store Application/Collection groups.

$Namespace = "Root\SMS\Site_" + $Sitename

Function Create-Collection($CollectionName)
{
    $CollectionArgs = @{
        Name = $CollectionName;
        CollectionType = "1";         # User Collection Type
        LimitToCollectionID = "SMS00002" # All Users Collection
    }
    Set-WmiInstance -Class SMS_Collection -Arguments $CollectionArgs -Namespace $Namespace | Out-Null
}

Function Update-Query($CollectionName) {

$QueryExperssion = 'select *  from  SMS_R_User where SMS_R_User.UserGroupName = "' + $Domain + '\\' + $CollectionName + '"'
$Collection = Get-WmiObject -Namespace $Namespace -Class SMS_Collection -Filter "Name='$CollectionName' and CollectionType = '1'"
 
#Validate Query syntax  
$ValidateQuery = Invoke-WmiMethod -Namespace $Namespace -Class SMS_CollectionRuleQuery -Name ValidateQuery -ArgumentList $QueryExperssion
 
If($ValidateQuery){
    $Collection.Get()

    #Create new rule
    $NewRule = ([WMIClass]"\\Localhost\$Namespace`:SMS_CollectionRuleQuery").CreateInstance()
    $NewRule.QueryExpression = $QueryExperssion
    $NewRule.RuleName = $CollectionName
 
    #Commit changes and initiate the collection evaluator                   
    $Collection.CollectionRules += $NewRule.psobject.baseobject
    $Collection.RefreshType = 6 # Enables Incremental updates
    $Collection.Put()
    $Collection.RequestRefresh()
    }
}

New-ADGroup -Server $DC -Name $CollectionName -Path $GroupOU -groupScope Global -Description $Description
Create-Collection $CollectionName
Update-Query $CollectionName

Let me know if you run into any problems.
Share this post if you liked it!