Query collections can be used in SCCM to target or give a quick counts of specific device states. For example we could create a query collection that contains all Windows 10 workstations.
Manually creating these types of collections can be quite time consuming especially if you manage multiple sites or hierarchies, therefore automatically generating these query based collections using a script can save a lot of time.
Credit where it’s due
This script borrows heavily from the Set Operational SCCM Collections script by Benoit Lecours from systemcenterdudes.com. Benoit created a wonderful script to create query collections that he commonly requires and shared it on the TechNet gallery.
I wanted to use Benoit’s script on a shared hierarchy and to remove the collection details from the script to allow us to easily manage the collections and queries later without revisiting the script. I decided to use a separate XML file to describe the collections and have included a working sample XML in the GitHub repository.
The script and the sample XML file can be found on GitHub here:
Script details and usage
The script can be run multiple times on the same environment and will only recreate the collections that are missing.
Create collections in Configuration Manager that are useful for operational monitoring.
Will accept an XML file of the desired collections and queries and create the collections in Configuration Manager.
3-character site code
Full or relative path to the XML file containing the collections and queries
.PARAMETER LimitingCollection[Optional] Sets the LimitingCollection used for new collections.
[Default] “All Systems”
.PARAMETER Organization[Optional] allows a default top level directory to be defined. This is useful
for a hierarchy that is shared between distinct departments or organizational units.
The value is also prepended to the collections names to allow each oraganisation to
create collections with unique names.
.PARAMETER FolderName[Optional] This is the folder that will contain the operational collections. It will
be in the root of device collections or within the Organization folder if defined.
.PARAMETER RecurInterval[Optional] Used in conjunction with RecurCount to set a collection update schedule.
Acceptable values are ‘Minutes’,’Hours’ or ‘Days’.
.PARAMETER RecurCount[Optional] Used in conjunction with RecurInterval to set a collection update schedule.
Author: Mark Allen
References: Benoit Lecours: Set Operational SCCM Collections
.\New-CMOperationalCollections.ps1 -SiteCode PR1 -Path .\MyCollections.xml
Will create the folder ‘Operational’ in the root node Device Collections.
The collections will be created in the Operational Folder.
EG Device Collections > Operational > <Collections from XML>
.\New-CMOperationalCollections.ps1 -SiteCode PR1 -Path .\MyCollections.xml -LimitingCollection “All Systems” -Organization “MyOrg”
Will create a “Test” folder in the root node of each object type; subfolders will be
created within the relevant folder. Collection names will be prepended by <Organization>
EG Device Collections > MyOrg > Operational > <Collections from XML>
Collections will be prepended by “MyOrg …”
.\New-CMOperationalCollections.ps1 -SiteCode PR1 -Path .\MyCollections.xml -RecurInterval “Days” -RecurCount “14”
A custom refresh interval will be set for all new collections.
Within the query tags we need to substitute the less than symbol < with the HTML equivalent <
You can view the working example here: https://github.com/markhallen/configmgr/blob/master/New-CMOperationalCollections/CMOperationalCollections.xml