Getting all Members of all Collections in a Folder in ConfigMgr

In our SCCM 2012 environment, we have a subfolder of device collections that are used for Software Updates.  I wanted to get a list of all the members of the different collections in that folder.  I saw a recent post by Kaido Järvemets on how to easily list folder objects using Powershell, so I decided to implement that code into a script that exports all the collections and their members in a ConfigMgr folder you specify, into a text file.

You need to get the ContainerNodeID from WMI as Kaido describes in his post, then add that and your site code to the script.

I want all the collections in the ‘Software Updates’ folder:

Updates

 

I run the script…

PS

Then I get a text file containing all the collections with their members. 🙂


$SiteCode = "PS1"
$FolderID = 16777221 # Use the WMI tool to get the FolderID

Write-Host "Getting Folder Name of Folder ID $FolderID"
$FolderName = Get-WmiObject -Namespace "ROOT\SMS\Site_$SiteCode" `
-Query "select * from SMS_ObjectContainerNode where ContainerNodeID='$FolderID'" | Select Name
$FolderName = $FolderName.Name
Write-Host "Getting Collections from '$FolderName'"
$CollectionsInSpecficFolder = Get-WmiObject -Namespace "ROOT\SMS\Site_$SiteCode" `
-Query "select * from SMS_Collection where CollectionID is
in(select InstanceKey from SMS_ObjectContainerItem where ObjectType='5000'
and ContainerNodeID='$FolderID') and CollectionType='2'"

$Collections = $CollectionsInSpecficFolder.Name

Write-host "Getting collection members for:"

Foreach ($Collection in $Collections) {

$Collection
$Col = Get-CMDeviceCollection -Name $Collection | Select Name, Comment, CollectionID, MemberCount
$Members = Get-CMDevice -CollectionName $Collection | Select Name | Sort Name
$Col,"**Members** ",$Members | ft -AutoSize | Out-File -FilePath C:\Scripts\Staging\SoftwareUpdatesMembers.txt -Append

}
Invoke-Item C:\Scripts\Staging\SoftwareUpdatesMembers.txt

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s