Get CM Objects from a Container

  ·  2 min read

Before you get started make sure you have read the following page. Get CM Containers

Now that you know how to find Containers and view what information they contain, lets get all the objects inside of the container located at ABC:\DeviceCollection\Folder1\Department

 1PS C:\Users\kassidy> Get-Item ABC:\DeviceCollection\Folder1\Department
 2
 3
 4PSPath                : AdminUI.PS.Provider\CMSite::siteserver.contoso.com\DeviceCollection\Folder1\Department
 5PSParentPath          : AdminUI.PS.Provider\CMSite::siteserver.contoso.com\DeviceCollection\Folder1
 6PSChildName           : Department
 7PSDrive               : ABC
 8PSProvider            : AdminUI.PS.Provider\CMSite
 9PSIsContainer         : True
10SmsProviderObjectPath : SMS_ObjectContainerNode.ContainerNodeID=618
11ContainerNodeID       : 618
12FolderFlags           : 0
13FolderGuid            : 4A56A29C-C24E-4255-870C-CB11EAD48DD8
14IsEmpty               : False
15Name                  : Department
16ObjectType            : 5000
17ObjectTypeName        : SMS_Collection_Device
18ParentContainerNodeID : 2
19SearchFolder          : False
20SearchString          :
21SourceSite            : ABC

We can do this by utilizing the Invoke-CMWmiQuiery cmdlet. You will need some information from the container to help create the WMI query. We will need the ContainerNodeID, ObjectType, ObjectTypeName, and an InstanceKey for that object. How do I find the instance key? I find the easiest way to find this is to run a query against just the ObjectTypeName. Look at the example below, in the SmsProviderObjectPath field the data is represented as [ClassName].[UniqueKey].

Note that the actual class is SMS_Collection not SMS_Collection_Device. If you look at the UserCollection container you’ll see that it shows as SMS_Collection_User.

1PS ABC:\> $Query = "select * from SMS_Collection"
2PS ABC:\> Invoke-CMWmiQuery -Query $Query
3
4SmsProviderObjectPath          : SMS_Collection.CollectionID="ABC0000B"

With the information gathered from above lets build our query to list the objects!

1$Query = "select * from SMS_Collection where CollectionID is in(select InstanceKey from SMS_ObjectContainerItem where ObjectType='5000' and ContainerNodeID='618')"
2Invoke-CMWmiQuery -Query $Query

If the query was created correctly you should see a lot of information scroll past. Store these in a variable to interact with them using the various cmdlets for collections. You can also query based on the path. This way appears to be rather resource heavy, and can run kinda slow. The previous example is my preferred method.

1$Query = "select * from SMS_Collection where ObjectPath = '/Folder1/Department'"
2Invoke-CMWmiQuery -Query $Query

In some cases you will need to convert your data to an IResultObject before you can use it with the available PowerShell cmdlets. This used to be the case anytime I ran Invoke-CMWmiQuery, but in some of the newer versions it appears to be doing the conversion for me. Take a look at ConvertTo-CMIResultObject and ConvertFrom-CMIResultObject

If you can’t figure out what format your object is, pipe your object into gm. Take note of the type name

1PS ABC:\> $items | gm
2
3
4   TypeName: IResultObject#SMS_Collection