SCCM Configmgr Remove Collection membership for Direct rule Collections using Powershell

 

This blog post is continuation to my previous post ‘Monitor collection evaluation's and remove incremental membership schedule for non-priority collections’ .More information can be found at http://eskonr.com/2019/01/sccm-configmgr-monitoring-collection-evaluations-and-change-update-membership-schedule-using-powershell/ .

In this post ,we will see how to improve the collection evaluation performance further by identifying list of collections with direct rule created that have membership (incremental and/or full update) enabled and use powershell to remove the membership schedule.

we will use the powershell script to identify 1)Direct based collections with Scheduled updates 2) Direct based collections with incremental updates + schedule updates 3) Direct based collections with incremental updates and remove the membership schedule

You can use different rules to configure the members of a collection in SCCM like Direct Rule ,Query rule, include and exclude. For more information on collection types, please refer https://docs.microsoft.com/en-us/sccm/core/clients/manage/collections/create-collections

Direct Rule: This is a static collection (manual changes required all the time) which means the membership does not change unless you remove a resource from SCCM.

Query Rule:This is dynamic collection and it dynamically update the membership of a collection based on a query that SCCM runs on a schedule.

image

As you see in the above snapshot ,the type of rule is Direct for direct (static) based collections and for query based collections,you will see Query in-place of direct.

Direct rule collections do not require the membership enabled because these collections are static and they never get update again and again unless user do manual changes .

If you are adding the resources to direct based collections using scripts then make sure you also use the syntax  to update the collection membership right there in the script .This is required for direct based collections if you did not enable the incremental or full schedule update.

Why is it required to update collection membership if you use script to add resources direct based collection (consider no membership enabled ) ? 

If you are adding the resources manually to the collection using GUI then it does refresh the collection automatically and get the resources into the collection for you where as through scripting ,it doesn't get the resources but just that ,the resources will be only added to the collection (GUI as i shown above) but if you open the collection ,it will be empty (this is what i noticed in my testing with scripting).

With this ,we now need to identify the collections that are direct based which have schedule membership enabled (incremental or/and full update) and remove the membership using powershell script.

Microsoft recommendation is Do not use incremental updates for a large number of collections. This configuration might cause evaluation delays when you enable it for many collections. The threshold is about 200 collections in your hierarchy. For more info refer here

Following is simple powershell script to query direct based collections with membership schedule enabled.

we are using built-in SCCM powershell cmdlet get-CMcollection to get all collections (user and device based) that have membership enabled (SMS_CollectionRuleDirect).

This script will save the list of direct based collections with schedule enabled to CSV file for reference at later stage. If you have any direct based collections to exclude from this ,you can use script that i posted in previous blog.

Depends on your infra and number of collections you have ,it might take sometime . For me ,it took 4 min to get 700+ collections that fall in the criteria.

Note: Before you run the script in production ,make sure you understand the requirement and also comment the $Collection.Put() so you can verify the list of collections you have infra and rerun the script by un-comment the line.

<#
Title: Update membership schedule for collections with direct based rule. Direct rule based collections do not need membership enabled.
Following are the collection membership values for refreshtype
1:No Scheduled Update
2:Full Scheduled Update
4:Incremental Update (Only)
6:Incremental and Full Update Scheduled
Author: Eswar Koneti
Blog:www.eskonr.com
Date:31-12-2018
#>

$scriptPath = $script:MyInvocation.MyCommand.Path
$CD = Split-Path $scriptpath #Get the current directory of the script that is located
$RefreshTypeto='1' #This is to convert the membership schedule ,1 is to remove the schedule.
$date = (get-date -f dd-MM-yyyy-hhmmss) #Get the current date and time when script runs
$collectionsfound="$CD\collections with direct rules-"+$date+".csv"
#This is our output file to pipe all collections with direct based rules for our reference later.

$ErrorActionPreference= 'silentlycontinue'

#Load SCCM module and map the powershell drive
Try
{
  import-module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)  #Import the powershell module . Make sure you have SCCM console installed on the PC that you run the script .
  $SiteCode=Get-PSDrive -PSProvider CMSITE #Get the sitecode
  cd ((Get-PSDrive -PSProvider CMSite).Name + ':')
}
Catch
{
  Write-Host "[ERROR]`t SCCM Module couldn't be loaded. Script will stop!"
  Exit 1
}
#Get all collections with membership enabled and direct membership rule only and export the collection details to CSV file for reference
get-CMcollection | where-object {$_.RefreshType -in ('2','4','6') -and ($_.Properties.CollectionRules.SmsProviderObjectPath -eq "SMS_CollectionRuleDirect")} `
|  select collectionID,Name | Export-CSV -NoTypeInformation $collectionsfound -append

foreach ($Coll in Import-Csv $collectionsfound ) #start the for loop for each each collection that found in SCCM and remove the collection membership schedule
{
$Collection = Get-CMCollection -CollectionId $Coll.collectionID
#write-host $Coll.collectionID $Coll.Name
  $Collection.RefreshType = $RefreshTypeto
  $Collection.Put()
}
write-host "Execution of script completed:" -foregroundcolor Yellow

you can also download the script from here

Following is the SQL code to pre-check and post-check the collections with membership schedule enabled.

select coll.SiteID,coll.CollectionName,
case when coll.CollectionType='1' then 'User' else 'Device' end as 'Collection Type'
from v_Collections_G coll
where coll.SiteID not in (select CRQ.collectionid from v_CollectionRuleQuery CRQ)
and coll.Flags in ('2','4','6')
group by coll.SiteID,coll.CollectionName,coll.CollectionType

 

Hope you enjoyed reading this article. See you in next post!

4 Responses to "SCCM Configmgr Remove Collection membership for Direct rule Collections using Powershell"

  1. in some cases collections may have both query and direct membership rules and just modified your script to pull only Direct membership rule defined in collections and exclude if collection is having both query and direct membership rules.

    #Get all collections with membership enabled and direct membership rule only and export the collection details to CSV file for reference
    $colM = get-CMcollection | where-object {$_.RefreshType -in ('2','4','6') }
    #-and ($_.Properties.CollectionRules.SmsProviderObjectPath -ne "SMS_CollectionRuleQuery")} `
    #| select collectionID,Name | Export-CSV -NoTypeInformation $collectionsfound -append
    $arm = @()
    foreach($co in $colM)
    {
    $Count = ((($co.Properties.CollectionRules.SmsProviderObjectPath) | select -Unique)).count
    if($Count -gt 1){continue}
    elseif($co.Properties.CollectionRules.SmsProviderObjectPath -eq 'SMS_CollectionRuleDirect'){$arm+=$co}
    }

    $arm | select collectionID,Name | Export-CSV -NoTypeInformation $collectionsfound -append

    Reply

Leave a Reply