Collections

This blog post will be about a question that I recently read on a forum and also already got a couple of times with different customers. That question is if it’s possible to simply create multiple smaller collections of one specific collection. This can be useful when a specific deployment has to be deployed in smaller groups then normally. Also, there are already a couple solution available in the community, like the SCCM Collection Splitter by Mickael Ponsot and the Planning an upgrade of an application by Jörgen Nilsson, but as always, not every solution fits every customer. In my case(s) these solution didn’t fit, as it shouldn’t have to much options and it shouldn’t rely on static properties.

That’s why I created a simple PowerShell script that requires a collection and a number of collections as input. Based on that input this PowerShell script creates collections with random, but equally divided, collection members. The rest of this post will explain this PowerShell script and shows an example.

Step 1: Get and set the start variables

The first step, like with every other script, is getting and setting variables to work with during the script. The collection name and the number of collections are input variables of this script and based on that input an array with devices will be created. I do this by using the Get-CMDevice cmdlet. Also, the number of devices per collection will be calculated. I do this by using the ceiling method of math. This will make sure that it will always round up, so we don’t have any leftovers after the script. This all together brings me to the following code:

Step 2: Create the new collections

The second step is to create the new collections. Starting from this point the script will go through a for-loop for as many times as the number of collections to be created (see the complete script). To make sure I create unique collections, I use the number of times it went through the for-loop as part of the collection name. Also, I simply use the New-CMDeviceCollection cmdlet to create the collections. This all together brings me to the following code:

Step 3: Get and add the collection members

The third step is to get the collections members and to add them to the newly created collections. To be able to select a number of devices I use the Get-Random cmdlet that allows me to just select a specific number of random devices from an array. After that I can simply loop through those devices and create a direct membership rule per device, by using the Add-CMDeviceCollectionDirectMembershipRule cmdlet. This all together brings me to the following code:

Step 4 : Prepare for the next collection

The fourth, and last step, is to make all the variables ready for the next round through the for-loop. This means I’ve got to make sure that the devices that are added to the previous collection, are removed from the the array with devices. Also, it’s important to recalculate the number of devices per collection. This is to make sure that I won’t end-up with a really small collection, or, in some cases, a collection without members. This all together brings me to the following code:

Result

At the end there’s nothing better then looking at a successful result. In the following screenshot the script is executed for the All Desktop and Server Clients collection to create four smaller (equally sized) collections.

This blog post will show how to approve, block, unapprove, or unblock clients in ConfigMgr 2012 via PowerShell. Three of these actions, approve, block and unblock, are also accessible via the console, but via PowerShell it is very easy to perform these actions for a whole collections. This creates the possibility to create a custom automatic approval in combination with for example a collection membership. The fourth action, unapprove, is not accessible via the console and only accessible via WMI (with some help of PowerShell).

Solution

In WMI there is the class SMS_Collection, which has the methods ApproveClients and BlockClients. These methods can be used to (un)approve and (un)block clients and they require both the same two parameters. They both require a boolean and an array as input. When the boolean is set to TRUE it will approve, or block, all the clients specified in the array and when the boolean is set to FALSE it will unapprove, or unblock, all the clients in the specified array. This brings us, per action, to the following code snippets:

Unblock

Complete example

Now after we know all the different invoke commands for the different actions, let’s use one of the actions for a complete example. Under here I created a small function to approve all clients in the specified collection. In short, this function creates an empty array, then queries for the CollectionId, then fills the array with all ResourceIds and at the end it will invoke the WMI method ApproveClients with the array as a parameter. This function needs as input the site code, the site server and the collection name.

A few weeks ago I’ve got the question if it is possible to speed-up the collection membership update after a new device was added to the database via my Import Comport Form. The short answer is, of course, yes! As this answer would make a really short blog post, I decided to put some more time in it. My first answer could have been, why can’t you wait on the incremental update cycle. As by default, the Use incremental updates for this collection is selected (and grayed-out) for the All Systems collection and the default Interval for Collection Membership Evaluation is five minutes (see this post for more information).

Solution

Now what can we do when either five minutes takes to long, or the default interval is changed to something longer and we can’t allow access to the ConfigMgr Console and we also don’t want to get called all the time. The answer is, of course, PowerShell! In WMI there is the class SMS_Collection, which has the method RequestRefresh. This method triggers a re-evaluation of the collection memberships and doesn’t require any parameters. The best part is, this can be done with a one-liner! I like short and simple code. All we need is the path to the collection and that brings us to the following code:

Import Computer Form

Somewhere this week, I will post an updated version of my Import Computer Form. This update will include this one-liner to re-evaluate the collection memberships after the new device is added to the database. So whatever configuration there may be, the collection will be directly updated.

A few months ago I released the first public version of my Import Computer Form. I’ve had some nice feedback about it and also some good ideas for added functionality. This update will mainly be about three things:

Cleaning-up old code, by merging a few lines and adjusting some queries.

Overview

These additions mean that this version gives the user the possibility to perform the following actions, without the need of access to and/ or a locally installed ConfigMgr console:

Fill in a Computer name.

Fill in a MAC Address.

Select an OS Deployment Collection.

Select a Primary User.

Import the Computer Information.

Add the User Device Affinity/ Primary User.

Close the form.

Inside information

I thought it might be nice to give some information about how easy it actually is to add this new functionality of adding a User Device Affinity/ Primary User. First start with looking for the class and the methods that hides the functionality that is needed. This can be done by, either browsing through WMI, or searching through the online reference on MSDN. As the screenshot shows, I like to use WMI Explorer. It also show that browsing to the class SMS_UserMachineRelationship reveals the method CreateRelationship, which again reveals its four parameters. For the exact information about the parameters it’s still necessary to look at MSDN. The “fun” part is that the input, of the parameters, is required in the order as shown in WMI. The order shown at the different MSDN articles is NOT the order needed as the input! After all the input is collected, the command line is “just” adding it all together in one Invoke-WmiMetod command, like this:

Note: I used exactly the same way to change my method, for importing a machine, into a “one-liner”. This made it to an Invoke-WmiMethod for the class SMS_Site and the method ImportMachineEntry, like this:

This week my post will be all about the end result of my series, about showing the Collection Details,of a device, via a PowerShell form. I changed a few small details and added another new tab, with more new information! This new tab shows all the General information about the collections, of which the device is a member. As there is (almost) no more information that can be added, about collection information that affects the device, this will be the last addition to the script/ form. In case someone still misses some important, or logical information, please let me know!

Collection Details Form

This script creates a nice form to show the Collection Details of the specified device. The form shows all of the following information about the collections, of which the device is a member:

(New!) Tab – General

(New!) The Name of the Collections.

(New!) The Id of the Collections.

(New!) The Limiting Collection of the Collections.

(New!) The Last Update of the Collections.

(New!) The Last Membership Change of the Collections.

Tab – Deployments

The Name of the Collections.

The Name of the targeted Deployments.

The State of the targeted Deployments.

The Type of the targeted Deployments.

Tab – Variables

The Name of the Collections.

The Name of the Variable.

The Value of the Variable.

(Updated!) The Type of the Variable.

Tab – Maintenance Windows

The Name of the Collections.

The Name of the Maintenance Window.

The Start Time of the Maintenance Window.

The Duration of the Maintenance Window.

The Recurrence of the Maintenance Window.

The Schedule of the Maintenance Window.

The Type of the Maintenance Window (including the, in R2, new type, for Software Updates).

Whether the Maintenance Window is enabled or not.

Tab – Power Management

The Name of the Collections.

The Name of the applied Non-Peak Power Plan.

The Name of the applied Peak Power Plan.

The Start Time of the applied Peak Power Plan.

The End Time of the applied Peak Power Plan.

The Wake-up Time of the devices in the Collection.

Note: When there is there is no Limiting Collection, no targeted Deployment, no Deployment State, no Collection Variable, no Maintenance Window, or no Power Plan then the empty field(s) will show N/A (as in Not Applicable).

Usage

This script can be used, either ‘standalone’, or as a right-click option. The command line usage examples can be found in the script and on the download site. To use this script as a right-click action, follow the next steps:

Create in both new folders a new XML file with the following content (keep an eye on the location of the PowerShell script, either place it on the location specified with the parameters, or change the location).

This week another addition to my series about showing the Collection Details,of a device, via a PowerShell form. This week I added another new tab, with more new information! This new tab show all the information about the applied Power Management settings, that affect the device. It’s also good to know that within the Power Management settings, there is an option Never apply power management settings to computers in this collection and when this option is selected the form will show a row with five times Never. So whenever a device shows a row like that, the other Power Management settings aren’t applicable anymore.

Collection Details Form

This script creates a nice form to show Collection Details of the specified device. At this moment it shows all of the following information about the collections (of which the device is a member):

Tab – Deployments

The Name of the Collections, of which the device is a member.

The Name of the targeted Deployments.

The State of the targeted Deployments.

The Type of the targeted Deployments.

Tab – Variables

The Name of the Collections, of which the device is a member.

The Name of the Collection Variable.

The Value of the Collection Variable.

The Type of the Variable (for now only Collection Variable).

Tab – Maintenance Windows

The Name of the Collections, of which the device is a member.

The Name of the Maintenance Window.

The Start Time of the Maintenance Window.

The Duration of the Maintenance Window.

The Recurrence of the Maintenance Window.

The Schedule of the Maintenance Window.

The Type of the Maintenance Window (including the, in R2, new type, for Software Updates).

Whether the Maintenance Window is enabled or not.

(NEW!) Tab – Power Management

(NEW!) The Name of the Collections, of which the device is a member.

(NEW!) The Name of the applied Non-Peak Power Plan.

(NEW!) The Name of the applied Peak Power Plan.

(NEW!) The Start Time of the applied Peak Power Plan.

(NEW!) The End Time of the applied Peak Power Plan.

(NEW!) The Wake-up Time of the devices in the Collection.

Note: When there is there is no targeted Deployment, no Deployment State (like with Client Settings), no Collection Variable, no Maintenance Window, or no Power Plan then the empty field(s) will show a star (*).

Power Plan

The tool will only show the name of the applied Power Management settings. In case there is a need for more information, then it’s good to know that it’s stored in a XML form (like the example under here). For an example of how to work with XML in PowerShell, take a look at an older post here.

Usage

This script can be used, either ‘standalone’, or as a right-click option. The command line usage is like this PowerShell.exe -ExecutionPolicy ByPass .\ShowCollectionDetails_v0_8p.ps1 -ResourceId <ResourceId> -SiteCode <SiteCode> -SiteServer <SiteServer>. To see how to use this as a right-click option, have a look at my previous post.

Availability

As of now this new version of my Collection Details Form is publicly available via the TechNet Galleries. Please let me know what you think of the form and what else you would like to see in it.

This week another addition to my series about showing the Collection Details,of a device, via a PowerShell form. This week I added another new tab, with more new information! This new tab show all the information about allthe Maintenance Windows that affect the device.

Collection Details Form

This script creates a nice form to show Collection Details of the specified device. At this moment it shows all of the following information about the collections (of which the device is a member):

Tab – Deployments

The Name of the Collections, of which the device is a member.

The Name of the targeted Deployments.

The State of the targeted Deployments.

The Type of the targeted Deployments.

Tab – Variables

The Name of the Collections, of which the device is a member.

The Name of the Collection Variable.

The Value of the Collection Variable.

The Type of the Variable (for now only Collection Variable).

(NEW!) Tab – Maintenance Windows

(NEW!) The Name of the Collections, of which the device is a member.

(NEW!) The Name of the Maintenance Window.

(NEW!) The Start Time of the Maintenance Window.

(NEW!) The Duration of the Maintenance Window.

(NEW!) The Recurrence of the Maintenance Window.

(NEW!) The Schedule of the Maintenance Window.

(NEW!) The Type of the Maintenance Window (including the, in R2, new type, for Software Updates).

(NEW!) Whether the Maintenance Window is enabled or not.

Note: When there is there is no targeted Deployment, no Deployment State (like with Client Settings), no Collection Variable, or no Maintenance Window then the empty field(s) will show a star (*).

Usage

This script can be used, either ‘standalone’, or as a right-click option. The command line usage is like this PowerShell.exe -ExecutionPolicy ByPass .\ShowCollectionDetails_v0_7p.ps1 -ResourceId <ResourceId> -SiteCode <SiteCode> -SiteServer <SiteServer>. To see how to use this as a right-click option, have a look at my previous post.

Availability

As of now this third version of my Collection Details Form is publicly available via the TechNet Galleries. Please let me know what you think of the form and what else you would like to see in it.

In my previous post I started a new series about showing the Collection Details of a device via a PowerShell form. This week I did some changes to the look-and-feel and added a new tab, with new information!. This new tab show all the information about all Collection Variables that affect the device.

Show Collection Details Form

This script creates a nice form to show Collection Details of the specified device. At this moment it shows all of the following information about the collections (of which the device is a member):

Tab – Deployments

The Name of the Collections, of which the device is a member.

The Name of the targeted Deployments.

The State of the targeted Deployments.

The Type of the targeted Deployments.

(NEW!) Tab – Variables

(NEW!) The Name of the Collections, of which the device is a member.

(NEW!) The Name of the Collection Variable.

(NEW!) The Value of the Collection Variable.

(NEW!) The Type of the Variable (for now only Collection Variable).

Note: When there is there is no targeted Deployment, or no Deployment State (like with Client Settings), or no Collection Variable, then the empty field(s) will show a star (*).

Usage

This script can be used, either ‘standalone’, or as a right-click option. The command line usage is like this PowerShell.exe -ExecutionPolicy ByPass .\ShowCollectionDetails_v0_6p.ps1 -ResourceId <ResourceId> -SiteCode <SiteCode> -SiteServer <SiteServer>. To see how to use this as a right-click option, have a look at my previous post.

Availability

As of today this second version of my Collection Details Form is publicly available via the TechNet Galleries. Please let me know what you think of the form.

In my previous post I started with creating a new PowerShell form to show the Collection Membership of a device. Last week I decided that I wanted to do more with it, so I putted it into a new “project” and form. From now on it’s called Collection Details, as that’s what it’s going to show, starting this week with the targeted Deployments. The coming weeks there will be additional parts, to this post and this “project”, with more and more information to be added to the Collection Details.

So what can we all expect from the Collection Details? Well, we all know the Collection Properties per collection, the Collection Details will show a lot of those properties (and more) on a device level. In general it will mean that, on completion, it will show all deployments and settings targeted to a collection and relevant for the device.

Show Collection Details Form

This script creates a nice form to show more Collection Details of the specified device. At this moment it shows all of the following information about the collections (of which the device is a member):

The Name of the Collections, of which the device is a member.

The Name of the targeted Deployments.

The Type of the targeted Deployments.

(NEW!)The State of the targeted Deployments.

Note: When there is there is no targeted Deployment, or no Deployment State (like with Client Settings), then the field will show a star (*).

Usage

This script can be used, either ‘standalone’, or as a right-click option. The command line usage is like this PowerShell.exe -ExecutionPolicy ByPass .\ShowCollectionDetails_v0_5p.ps1 -ResourceId <ResourceId> -SiteCode <SiteCode> -SiteServer <SiteServer>. To see how to use this as a right-click option, have a look at my previous post.

Availability

As of today the first version of my Show Collection Details Form is publicly available via the TechNet Galleries. Please let me know what you think of the form.

Last week someone triggered me that it wasn’t possible to ‘just’ see all the collections that a device is a member of. The only thing I could show, and tell, him, at that moment, was the Deployments –tab in the Properties of the device. The problem with this is that it only show the collections, of which the device is a member, with a deployment targeted. So it misses all the collections, either without an deployment targeted, or with client/ antimalware settings targeted. This triggered me to create a small PowerShell script to show the Collection Membership of a specific device in a nice form.

Show Collection Membership Form

This script creates a nice form to show the Collection Membership of the specified device. It shows all the extra information for the following collections:

Collections targeted with (all types of) Deployments,

Collections targeted with Client Settings,

Collections targeted with Antimalware Policies,

Collections targeted with nothing!

Usage

This script can be used, either ‘standalone’, or as a right-click option. The command line usage examples can be found in the script and on TechNet (link is provided at the end of this post). To use this script as a right-click action, follow the next steps:

Create in both new folders a new XML file with the following content (keep an eye on the location of the PowerShell script, either place it on the location specified with the parameters, or change the location).

Award

Subscribe to updates

About

I’m Peter van der Woude, born in 1983 and I’m living together with my wife and two sons in the Netherlands.

Currently I work for KPN Consulting. At this moment my main focus is Enterprise Client Management via Microsoft Intune and/ or System Center Configuration Manager (ConfigMgr 2007/ 2012/ CB) and I love it!