SCCM 2012 SP1: Create Install, Uninstall Collections and AD Groups with powershell.

One of the big new features in SCCM 2012 SP1 is is the PowerShell module. Powershell is really great for automation.

If you are using AD group membership together with dynamic collection rules to target and deploy applications, you probably have spent hours creating collections, ad groups and queries.

I have created a simple PowerShell script to automate this task, feel free to modify the script to fit your needs.

For the script to be able to create AD Group, install the active Directory PowerShell module on the server or client that you run your ConfigMgr Console from. You will also need to enable AD Group discovery in SCCM.

Code: Download here

  • Two collections is created for the application.

  • Install collection Properties

  • Uninstall collection properties

3 thoughts on “SCCM 2012 SP1: Create Install, Uninstall Collections and AD Groups with powershell.

  1. I am trying to get this script to work, but am having issues.. I removed the line about creating the AD group since we have them created and do not want a script creating them. I can get as far down as creating the collections and then is gives this error. “New_CMDeviceCollection : This command cannot be run from the current drive. To run this command you must first connect to a Configuration Manager drive.”
    Where exactly does this need to connect to? I am running the script on our CAS server. SCCM is installed on the D: drive. I have the script changing to the D: drive right before this section..
    Thanks

  2. Sorry, I guess I could have included the modified version of the script I am running..
    #Make sure you the active directory module for windows powershell installed
    $CMfolder = split-path $env:SMS_ADMIN_UI_PATH
    Import-Module “D:\SCCM2012\AdminConsole\bin\ConfigurationManager.psd1”
    Pause
    Import-Module activedirectory
    Pause

    $AppGroupOU = “OU=ClientApps,OU=Applications,dc=cag,dc=conagrafoods,dc=net”
    Pause
    $DC = “DCNEOMA17”
    Pause
    $AppName = Read-Host “Enter application name”
    Pause
    $LimitingCollection = “All Systems”
    Pause
    $AppInstallCollQuery = “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.SecurityGroupName like ‘%APP_$AppName'”
    Pause

    D:
    Pause

    New-CMDeviceCollection –Name “$AppName – Install” –LimitingCollectionName “$LimitingCollection” -RefreshType ConstantUpdate
    Pause
    Add-CMDeviceCollectionQueryMembershipRule -CollectionName “$AppName – Install” -QueryExpression “$AppInstallCollQuery” -RuleName “$AppName – Install”
    Pause
    New-CMDeviceCollection –Name “$AppName – Uninstall” –LimitingCollectionName “$LimitingCollection” -RefreshType ConstantUpdate
    Pause
    Add-CMDeviceCollectionIncludeMembershipRule -CollectionName “$AppName – Uninstall” -IncludeCollectionName “$LimitingCollection”
    Pause
    Add-CMDeviceCollectionExcludeMembershipRule -CollectionName “$AppName – Uninstall” -ExcludeCollectionName “$AppName – Install”
    Pause

  3. Sorry, I guess I could have included the modified version of the script I am running..
    #Make sure you the active directory module for windows powershell installed
    $CMfolder = split-path $env:SMS_ADMIN_UI_PATH
    Import-Module “D:\SCCM2012\AdminConsole\bin\ConfigurationManager.psd1”
    Pause
    Import-Module activedirectory
    Pause

    $AppGroupOU = “OU=ClientApps,OU=Applications,dc=cag,dc=XXXXXX,dc=net”
    Pause
    $DC = “DCNEOMA17”
    Pause
    $AppName = Read-Host “Enter application name”
    Pause
    $LimitingCollection = “All Systems”
    Pause
    $AppInstallCollQuery = “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.SecurityGroupName like ‘%APP_$AppName'”
    Pause

    D:
    Pause

    New-CMDeviceCollection –Name “$AppName – Install” –LimitingCollectionName “$LimitingCollection” -RefreshType ConstantUpdate
    Pause
    Add-CMDeviceCollectionQueryMembershipRule -CollectionName “$AppName – Install” -QueryExpression “$AppInstallCollQuery” -RuleName “$AppName – Install”
    Pause
    New-CMDeviceCollection –Name “$AppName – Uninstall” –LimitingCollectionName “$LimitingCollection” -RefreshType ConstantUpdate
    Pause
    Add-CMDeviceCollectionIncludeMembershipRule -CollectionName “$AppName – Uninstall” -IncludeCollectionName “$LimitingCollection”
    Pause
    Add-CMDeviceCollectionExcludeMembershipRule -CollectionName “$AppName – Uninstall” -ExcludeCollectionName “$AppName – Install”
    Pause

Leave a Reply

Your email address will not be published.