I recently set up a whole new SCCM 2012 environment and we needed to create collections for a lot of OUs containing the computers.The script below will run through an OU structure and create device collections for each OU and sub OU’s.
To run the script open your SCCM console and press the drop down menu next to the home button and choose “Connect via Windows Powershell”.
This will establish a connection to the SCCM and allow you to use some special CM powershell functions which is utilized in this script.
To avoid warnings about the script not being digitally signed run this command prior to executing the script it self.
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Now you can run the script.
It takes 1 mandatory argument and 3 optional which if not specified will default to the values they have in the script.
The mandatory argument is the SearchBase which is the top level OU from which you want to create the OU based Collections. The Optional arguments are LimitingCollection, SearchScope and RefreshType.
To run the script in its most simple form:
New-OUDeviceCollection.ps1 -SearchBase “OU=Computers,OU=Company,DC=OMG,DC=local”
This will create a collection for each of the OU’s under omg.local\Company\Computers – excluding the Computers container it self.
New-OUDeviceCollection.ps1 -SearchBase “OU=Computers,OU=Company,DC=OMG,DC=local” -LimitingCollection “All Windows 10 Computers” -SearchScope Subtree -RefreshType 2
This will create a collection for each of the OU’s under omg.local\Company\Computers – including the Computers container it self. It will use “All Windows 10 Computers” as the limiting Collection and it will use periodic update.
# Name: New-OUDeviceCollection # Arguments: 4 (1 Mandatory, 3 Optional) # 1. SearchBase (String, Mandatory): The top level OU from where you want to create OU Collections. The DistinguisedName attribute of the OU from AD is used. # 2. LimitingCollection (String, Optional): The Collection to limit the new collections to. Default: All Desktop and Server Clients # 3. SearchScope (String, Optional): Subtree, OneLevel or Base. Default: OneLevel # 4. RefreshType (String, Optional): 1 (Manual),2 (Periodic), 4(CONSTANT_UPDATE) - Default: 4 # # Description: This script will run through an OU and create device collections for each OU and sub OU's # depending on what you specify in SearchScope. param( [string]$SearchBase, [string]$LimitingCollection = 'All Desktop and Server Clients', [string]$SearchScope = 'OneLevel', [string]$RefreshType = '4' ) $OUS = Get-ADOrganizationalUnit -SearchBase $SearchBase -SearchScope $SearchScope -Filter * -Properties Canonicalname foreach ($OU in $OUS) { $OUName=$OU.Name $Canonical=$OU.CanonicalName New-CMDeviceCollection -Name "$OUName" -LimitingCollectionName $LimitingCollection -RefreshType $RefreshType Add-CMDeviceCollectionQueryMembershipRule -CollectionName "$OUName" -QueryExpression "select SMS_R_SYSTEM.ResourceID, SMS_R_SYSTEM.ResourceType, SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemOUName = '$Canonical'" -RuleName "$OUName OU" }
Recent Comments