How to add computers listed in a text file to a collection

Using the new ConfigMgr 2012 PowerShell cmdlets, there are three different ways to add computers to a device collection.  Just like in the console, you can add computers via a direct membership rule (Add-CMDeviceCollectionMembershipRule), a query membership rule (Add-CMDeviceCollectionQueryMembershipRule) and also an include collection membership rule (Add-CMDeviceCollectionIncludeMembershipRule).  In order to use these cmdlets, the collection already needs to be created.  In my previous post How to quickly create Collections using New-CMDeviceCollection, I demonstrated one way of creating device collections based off of a for loop and using each loop as an index number in the collection name.

In this post, since we will be reading a list of computers from a text file, we will use the direct membership rule cmdlet Add-CMDeviceCollectionMembershipRule to add them to the collection.  Using this cmdlet, you can add a device resource object to one or more device collections.  Since collection names are unique in CM 12, it is easiest to reference the collection that you want to add objects to using the -CollectionName parameter.  For the device that we are trying to add, the easiest way is to get the resource ID and use the -ResourceId parameter.  We can get the resource ID of a device called PC01 using the Get-CMDevice cmdlet:

(Get-CMDevice -Name PC01).ResourceID

Using the Add-CMDeviceCollectionDirectMembershipRule, we can add PC01 to the collection called Collection_01 with the following command:

Add-CMDeviceCollectionDirectMembershipRule  -CollectionName Collection_01 -ResourceId $(get-cmdevice -Name PC01).ResourceID

Now we just need to read the computers in from a text file and add them to our collection. I created a text file called Collection_01.txt in D:\Collections and have added PC01, PC02, PC03 and PC99 (this one is not a valid client in my ConfigMgr lab). We can read these computers into a variable called $computers using the following command:

$computers = Get-Content "D:\Collections\Collection_01.txt"

If we add the above commands to the following loop, we can loop through the variable $computers and add each computer to the specified collection:

foreach($computer in $computers) {
   Add-CMDeviceCollectionDirectMembershipRule  -CollectionName Collection_01 -ResourceId $(get-cmdevice -Name $computer).ResourceID
}

If the computer name is already defined as a direct membership rule or it is not a valid client, it will produce an error. We can account for these errors by using try/catch functionality built into PowerShell. Inside of the for loop we can add:

foreach($computer in $computers) {
   try {
      Add-CMDeviceCollectionDirectMembershipRule  -CollectionName $collectionname -ResourceId $(get-cmdevice -Name $computer).ResourceID
       }
   catch {
      "Invalid client or direct membership rule may already exist: $computer" | Out-File "D:\Collections\Collection_01_error.log" -Append
       }
}

Now, wouldn’t it be great if we could create the collections based on the file names that contained a list of computers and automatically add the computers to the correct collection? You bet – the following will do just that. It will look in D:\Collections\ for all text files, create a collection based on the file names (minus the file extension), create direct membership rules for each computer name of the respective collection, and create a log file in the same directory with the collection name of any invalid collections, clients or rules.

#Create Collections and Membership Rules from Text Files
#Author: Mike Terrill
#Set path to collection directory
$collectiondir = "D:\Collections\"

#Pull only .TXT files into array
$filenames = Get-ChildItem $collectiondir* -include *.txt

for ($x=0; $x -lt ($filenames.Length); $x++) {
    $collectionname = $filenames.Name[$x].Split(".")[0]
    $collectionname
    #Add new collection based on the file name
    try {
        New-CMDeviceCollection -Name $collectionname -LimitingCollectionName "All Systems"
        }
    catch {
        "Error creating collection - collection may already exist: $collectionname" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
        }

    #Read list of computers from the text file
    $computers = Get-Content $filenames[$x]
    foreach($computer in $computers) {
        try {
            Add-CMDeviceCollectionDirectMembershipRule  -CollectionName $collectionname -ResourceId $(get-cmdevice -Name $computer).ResourceID
            }
        catch {
            "Invalid client or direct membership rule may already exist: $computer" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
            }
    }
}

As always, be sure to test any scripts or cmdlets in a test environment. Automation can be a great thing if used correctly and a very bad thing if used incorrectly.

Disclaimer:
Your use of these example scripts or cmdlets is at your sole risk. This information is provided “as-is”, without any warranty, whether express or implied, of accuracy, completeness, fitness for a particular purpose, title or non-infringement. I shall not be liable for any damages you may sustain by using these examples, whether direct, indirect, special, incidental or consequential.

Originally posted on http://miketerrill.net/

email

Written by , Posted .