Full disclosure, the script below is based on the script by Jonathan Lefebvre at System Center Dudes.
The script below will create a folder structure “Operational\Boundary Groups based collection”. If a collection already exists, a message is disabled on the screen, otherwise, the collection is created and move to the folder.
The goal of the changes is to make it so the script can be scheduled to run nightly or weekly. This makes the creation of collections based on boundaries no longer manual.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
############################################################################# # Author : Jonathan Lefebvre-Globensky # Website : www.SystemCenterDudes.com # Twitter : @jlefebvregloben, @SCDudes # # Version : 1.0 # Created : 2020/04/23 # Modified : # # Purpose : This script create collections based on each found Boundary groups in an environment. # Blog post related : # Previous script to create collections with folder structure :https://www.systemcenterdudes.com/powershell-script-create-set-maintenance-collections # Want more default operationnal collection? See Benoit Lecours powershell script --> https://gallery.technet.microsoft.com/Set-of-Operational-SCCM-19fa8178 # # # Updated by: Jim Webb # Modified date: 12/3/21 # ############################################################################# #Load Configuration Manager PowerShell Module Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1') #Get SiteCode $SiteCode = Get-PSDrive -PSProvider CMSITE Set-location $SiteCode":" #Set the prefix for collections to be created $CollectionPrefix='BG - ' #Define possible limiting collections $LimitingCollection = "All Systems" #$LimitingCollection = "All Workstations" #$LimitingCollection = "All Workstations - Admin" #$LimitingCollection = "All Servers" #Refresh Schedule $Schedule = New-CMSchedule –RecurInterval Days –RecurCount 1 If (!(Test-Path -Path "$($SiteCode.name):\DeviceCollection\Operational\Boundary Groups based collection")) { #Create sub folder for Boundary groups collection new-item -Name 'Boundary Groups based collection' -Path $($SiteCode.Name+":\DeviceCollection\Operational") #$FolderPath = $SiteCode.name+":\DeviceCollection\Boundary Groups based collection" } $FolderPath = $SiteCode.name+":\DeviceCollection\Operational\Boundary Groups based collection" #Get the list of all Boundary groups $BoundaryGroups=Get-CMBoundaryGroup foreach($BoundaryGroup in $BoundaryGroups) { #Define Collection name $CollectionName=$CollectionPrefix+$BoundaryGroup.Name #Write-Host $CollectionName Try { If (!(Get-CMDeviceCollection -Name $CollectionName)) { #Create Collections New-CMDeviceCollection -Name $CollectionName -Comment "Collection based on boundary group of the same name" -LimitingCollectionName $LimitingCollection -RefreshSchedule $Schedule -RefreshType 2 | Out-Null Write-host *** Collection $CollectionName created *** #DEfine Collection query $CollectionQuery="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.ResourceId in (select resourceid from SMS_CollectionMemberClientBaselineStatus where SMS_CollectionMemberClientBaselineStatus.boundarygroups like '%"+$BoundaryGroup.Name+"%') and SMS_R_System.Name not in ('Unknown') and SMS_R_System.Client = '1'" # Write-Host $CollectionQuery #Add collection query Add-CMDeviceCollectionQueryMembershipRule -CollectionName $CollectionName -QueryExpression $CollectionQuery -RuleName "Boundary Group" |out-null # Moving collection to folder Try { Move-CMObject -FolderPath $FolderPath -InputObject (Get-CMDeviceCollection -Name $CollectionName) Write-host *** Collection $CollectionNAme moved to the $folderPath folder *** } Catch { Write-host -ForegroundColor Red ("There was an error moving the: " + $CollectionName + " collection.") } } else { Write-Warning "Collection `"$CollectionName`" already exists." } } Catch { Write-host -ForegroundColor Red ("There was an error creating the: " + $CollectionName + " collection. Possible cause is that there's already a collection with that name.") } } |