ConfigMgr 2012 – Retrieving Objects in a Folder with PowerShell

ConfigMgr 2012 – Retrieving Objects in a Folder with PowerShell

(Originall posted 3/21/2014. Reposting for reference)

 

The introduction of folders in the ConfigMgr 2012 console made for a great way to organize content within the ConfigMgr site.  To see what objects might be under any given folder in the console is simple…but what if you want to do it programmatically with PowerShell?

Folders in ConfigMgr can be accessed through the WMI namespace for the site (Root\SMS\Site_XXX) in two classes:

SMS_ObjectContainerNode – Contains the list of all folder objects in the site.  The Name property corresponds to the display name of the folder.

SMS_ObjectContainerItem – Contains the list of all associations between an object and a folder (ie – folder membership). The InstanceKey property corresponds to the content ID of the object (ex – CollectionID for a Device Collection).

By leveraging this information along with the ConfigMgr 2012 Powershell module, we can retrieve a list of objects in a folder with a few quick lines of code.  Consider the following example for retrieving all Device Collections in a particular folder:

$SiteCode = ‘PS1’

$FolderName = ‘Test’

Import-Module (Join-Path (Split-Path $env:SMS_ADMIN_UI_PATH –parent) ConfigurationManager.psd1)

set-location ${SiteCode}:

$FolderObj = Get-WmiObject -Class SMS_ObjectContainerNode -Namespace Root\SMS\Site_$SiteCode -filter “Name=’$FolderName‘” | foreach { $_.ContainerNodeID }

Get-WmiObject -Class SMS_ObjectContainerItem -Namespace Root\SMS\Site_$SiteCode -filter “ContainerNodeID=’$FolderObjID‘” | ForEach-Object {

    Get-CMDeviceCollection -CollectionID $_.InstanceKey | foreach { $_.Name }

}

Checking aganist the ConfigMgr console, we can see that it correctly listed the Device Collections in the Test folder:

This method can be used for virtually any content type organized in folders.  For example, if I have a series of folders under the Task Sequences node…

…I can retrieve a list of those folders…

…and then retrieve the Task Sequences stored in one of the folders.

Combined with the Move-CMObject cmdlet, you can easily script out massive reorganization of console content with just a small amount of code!

Many thanks to David O’Brien for some pointers on efficiency in the scripting. Check out his website for more great ConfigMgr PowerShell scripts.

 

Chrysanth WebStory WebStory: Get a super Twitter client!

Leave a Reply

Your email address will not be published. Required fields are marked *