Create a dynamic ConfigMgr Collection with PowerShell

The code in this post is to create the “All Windows 8.1 Clients” device collection in Chapter 5 of “Learn ConfigMgr 2012 in a Month of Lunches

The collection is a standard Device Collection with a scheduled query as well as dynamic updates, and is populated using a WQL-based query.

Change the variables around to use this code in your own environment 🙂

# Define Configuration Manager variables
 
$CollectionName = 'All Windows 8.1 Clients'
$LimitingCollectionID = (Get-CMDeviceCollection | Where-Object {$_.Name -eq 'All Desktop and Server Clients'}).CollectionID
$CMSchedule = New-CMSchedule -Start '2014/01/01 00:00:00' -RecurInterval Days -RecurCount 1
 
# 1.0 Create new Device Collection
 
New-CMDeviceCollection -Name $CollectionName `
  -LimitingCollectionId $LimitingCollectionID `
  -RefreshType Both `
  -RefreshSchedule $CMSchedule
 
# 1.1 Create new Collection Query Rule
 
$Collection = Get-CMDeviceCollection -Name $CollectionName
 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName $CollectionName `
  -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.OperatingSystemNameandVersion like "%Workstation 6.3%"' `
  -RuleName 'Windows 8.1 Operating System'
 
# 1.2 Update the collection
 
Invoke-CMDeviceCollectionUpdate -Name $CollectionName

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>