{"id":2786,"date":"2014-03-23T15:04:51","date_gmt":"2014-03-23T15:04:51","guid":{"rendered":"https:\/\/pipe2text.com\/?page_id=2786"},"modified":"2014-03-23T20:18:31","modified_gmt":"2014-03-23T20:18:31","slug":"using-powershell-to-create-multiple-device-collections-in-sccm-2012-r2-based-on-active-directory-organizational-units-ous","status":"publish","type":"page","link":"https:\/\/pipe2text.com\/?page_id=2786","title":{"rendered":"Using PowerShell to Create Multiple Device Collections in SCCM 2012 R2 Based on Active Directory Organizational Units (OUs)"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>In this article I will be discussing and providing a PowerShell script I recently wrote which will query OUs in Active Directory and create device collections in System Center Configuration Manager 2012 based on the OU name. All machines that have the SCCM agent installed and are located in those OUs will be populated into the newly created collections. This can save a great deal of time if you have many device collections to create. You will need to have the \u201cActive Directory\u201d and \u201cConfiguration Manager\u201d modules for PowerShell\u201d installed on the computer you are running this script from. The session must also be changed to point to the Configuration Manager PSDrive. The script needs\u00a0to be run using the x86 version of PowerShell . There are a few ways to do this but for the purpose of this article, I will go thru the steps I feel are the easiest to set up the prerequisites.<\/p>\n<p>Log on to a server where the Configuration Manager Console installed and install the Active Directory Module for PowerShell on this machine so you can access those cmdlets. Next, Open the Configuration Manager Console, click the down arrow in the top left corner and choose \u201cConnect via Windows PowerShell\u201d as shown below.<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image1.png\" \/><\/p>\n<p>A PowerShell window will now open which will have the Configuration Manager Module for PowerShell loaded and the session will be pointed to the Configuration Manager PSDrive as shown below. Since PowerShell 3.0 was required for the Configuration Manager PowerShell module, the Active Directory Module will load in automatically when one of its cmdlets is accessed from the script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image2.png\" width=\"695\" height=\"251\" \/><\/p>\n<p>Now\u00a0 copy the following code into a notepad and save it with a .ps1 extension or download the full script here <a href=\"https:\/\/pipe2text.com\/wp-content\/uploads\/2014\/03\/New-OUBasedDeviceCollection.zip\">New-OUBasedDeviceCollection<\/a>. Then read the following sections for usage and parameter information before running the script.<\/p>\n<address>\n<pre class=\"brush:powershell\">param(\r\n[string]$searchbase,\r\n[string]$append,\r\n[string]$LimitingCollection = 'All Desktop and Server Clients',\r\n[string]$SearchScope = 'Subtree',\r\n[string]$RefreshType = 'Manual'\r\n)\r\n$OUS = Get-ADOrganizationalUnit -searchbase $searchbase -SearchScope $SearchScope -Filter * -Properties canonicalname\r\nforeach ($OU in $OUS) \r\n {\r\n    $Name=$OU.Name\r\n    $Canonical=$OU.CanonicalName\r\n    New-CMDeviceCollection -Name \"$Name $append\" -LimitingCollectionName $LimitingCollection -RefreshType $RefreshType\r\n    Add-CMDeviceCollectionQueryMembershipRule -CollectionName \"$Name $append\" -QueryExpression \"select SMS_R_SYSTEM.ResourceID, SMS_R_SYSTEM.ResourceType,\r\n    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\r\n   = '$Canonical'\" -RuleName \"$Name $append OU\"\r\n }<\/pre>\n<\/address>\n<address>\u00a0<\/address>\n<p><span style=\"text-decoration: underline;\"><strong>Parameters Explained<\/strong><\/span><\/p>\n<p>For my example, I will be copying the script I named\u00a0&#8220;New-OUBasedDeviceCollection.ps1&#8221; to the \u201cC:\\SCCMScript\u201d directory. Now before we run the above script, I need to explain how to use the parameters so it will properly execute. There are 5 parameters that can be specified.<\/p>\n<p>Searchbase \u2013 It is necessary that you specify this parameter. With this parameter you specify the distinguishedname of the top level OU where you want to start your search for collection creation. In this example I will use &#8220;OU=Servers,DC=pipe2text,DC=com&#8221;.<\/p>\n<p>LimitingCollection \u2013This parameter is used to define the Limiting Collection for your device collection. If you do not specify this parameter it defaults to the &#8220;All Desktops and Server Clients&#8221; device collection.<\/p>\n<p>SearchScope \u2013 This parameter tells the script how deep under the initial OU to search for OUs to create device collections (In this example the \u201cServers OU\u201d). There are 3 options to choose from and they are &#8220;Base&#8221;, &#8220;OneLevel&#8221; or &#8220;Subtree&#8221;. If you do not specify this parameter it will default to Subtree. In my example Subtree would create a collection group based on every OU located under the Servers OU (Including the Server OU). For the OUs in the illustration below you would have a collection group created for the Servers, Print, File, Mail, and SQL OUs.<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image3.png\" \/><\/p>\n<p>Append- The append parameter is used to append any text you would like to add at the end of the collection group name and the rule name. For example, take the above illustration, if you specified the word \u201cServers\u201d it would append the word Servers for each collection name and rule name as shown below. As you can see, the collection name is called \u201cFile Servers\u201d and the Rule Name is called \u201cFile Servers OU\u201d. If you do not specify the Append parameter no text will be appended (In this example the collection name would be \u201cFile\u201d and rule name would be \u201cFile OU\u201d if not specified).<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image4.png\" \/><\/p>\n<p>RefreshType- This parameter lets you set the option to \u201cUse incremental updates for this collection\u201d. Choose either \u201cManual\u201d or \u201cConstantUpdate\u201d. Choosing Manual leaves the box unchecked (shown below). Choosing ConstantUpdate checks the box. If this parameter is not specified the default is Manual.<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image5.png\" \/><\/p>\n<p>Now that I have explained all of the parameters, let\u2019s run the script.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Running the Script<\/strong><\/span><\/p>\n<p>In my example I will be creating Collection Groups based the &#8220;OU=Servers,DC=pipe2text,DC=com&#8221; Organizational Unit and every OU underneath it. The Limiting collection will be \u201cAll Desktop and Server Clients\u201d, we will append the word \u201cServers\u201d and check the box to \u201cUse incremental updates for this collection\u201d. My script is located in the \u201cc:\\sccmscript\u201d directory. Go back to the PowerShell Console we opened from the SCCM Console earlier and run the following command changing the parameters so they are suitable for your environment. Specify the full path to the location where you saved the script. Remember,<span style=\"color: #000000;\"> <span style=\"font-size: medium;\">The session must point to the Configuration Manager PSDrive for the script to run properly.<\/span><\/span><\/p>\n<p>The Command:<\/p>\n<p>C:\\SCCMScript\\New-OUBasedDeviceCollection.ps1 -searchbase &#8220;OU=Servers,DC=pipe2text,DC=com&#8221; -LimitingCollection &#8220;All Desktop and Server Clients&#8221; -SearchScope Subtree -Append Server -RefreshType ConstantUpdate<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image6.png\" width=\"860\" height=\"90\" \/><\/p>\n<p>Output for each created device collection will look similar to the following illustration:<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image7.png\" \/><\/p>\n<p>Now if you look under \u201cDevice Collections\u201d under the \u201cAssets and Compliance\u201d node in the SCCM 2012 Console you will see your new device collections as shown below (You may need to refresh the console before you see them).<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-create-multiple-collection-groups-based-on-organizational-units\/image8.png\" \/><\/p>\n<p>I hope this helps. Remember, always test this and other scripts in a lab before deploying to production. If you have any questions of feedback, please leave a comment. Thanks!<\/p>\n<p>Related Links:<\/p>\n<p><strong><a title=\"Using PowerShell Export Devices In Need of the SCCM 2012 Client to a CSV File\" href=\"https:\/\/pipe2text.com\/?page_id=2402\">Using PowerShell Export Devices In Need of the SCCM 2012 Client to a CSV File<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, In this article I will be discussing and providing a PowerShell script I recently wrote which will query OUs in Active Directory and create device collections in System Center Configuration Manager 2012 based on the OU name. All &hellip; <a href=\"https:\/\/pipe2text.com\/?page_id=2786\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"class_list":["post-2786","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2786"}],"version-history":[{"count":39,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2786\/revisions"}],"predecessor-version":[{"id":2846,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2786\/revisions\/2846"}],"wp:attachment":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}