Search code examples
c#sccm

SCCM Get Items in a Folder


I'm completly new to SCCM and I am confused by the object model. I have a folder like this:

SCCM folder

And I want to get a list of the object inside it:

  • APP0001V01 (etc)
  • APP0001V02 (etc)
  • etc

The code I have so far is:

       public void GetCollectionTest2()
    {
        var con = this.scomService.Connect(Server, UserName, Password);

        string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
        IResultObject packages = con.QueryProcessor.ExecuteQuery(query);

        foreach (IResultObject ws in packages)
        {

            foreach (IResultObject item in ws.Properties)
            {
                Debug.Print(item.ToString());
            }
            string query2 = "SELECT * FROM SMS_FullCollectionMembership WHERE InstanceKey = '" + ws["InstanceKey"] + "'";
            IResultObject packages2 = con.QueryProcessor.ExecuteQuery(query2);
            foreach (IResultObject ws2 in packages2)
            {
                Debug.Print(ws2["Name"].StringValue);
            }
        }
    }

I think that

"select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";

Is returning the folder I want but when I try to get the contents I just keep drawing a blank.

What should I be doing?

Update

Thanks to the reply from Xin Guo I now have:

        public void GetCollectionTest2()
    {
        var con = this.scomService.Connect(Server, UserName, Password);

        string query = "select * from SMS_ObjectContainerItem  where ContainerNodeID = '16777219'";
        IResultObject packages = con.QueryProcessor.ExecuteQuery(query);

        foreach (IResultObject ws in packages)
        {

            foreach (IResultObject item in ws.Properties)
            {
                Debug.Print(item.ToString());
            }
        }
    }

This seems to give me all the item but how do I get their names?

At the moment I can only return these:

instance of SMS_ObjectContainerItem
{
    ContainerNodeID = 16777219;
    InstanceKey = "0010047C";
    MemberGuid = "C8A66344-B7E8-451B-A4EF-9BFB3B3E228C";
    MemberID = 16778256;
    ObjectType = 5000;
    ObjectTypeName = "SMS_Collection_Device";
    SourceSite = "";
};

I assume I need to link to use an ID to look the name up somewhere else but I can't find any documentation on this?


Solution

  • Ahh finally worked it out thanks to a good rummage through WMI Explorer. In the diagram above the folders on the left are Containers & those on the right are collections.

    So the code I wanted was:

        public void GetCollectionTest2()
        {
            var con = this.scomService.Connect(Server, UserName, Password);
    
            string query = "select * from SMS_ObjectContainerItem  where ContainerNodeID = '16777219'";
            IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
    
            foreach (IResultObject ws in packages)
            {
                Debug.Print(ws["InstanceKey"].StringValue);
    
                string query2 = "SELECT * FROM SMS_Collection WHERE CollectionID='"+ ws["InstanceKey"].StringValue +"'";
    
                //// Run query.
                IResultObject colResultObject = con.QueryProcessor.ExecuteQuery(query2);
    
                foreach (IResultObject ws2 in colResultObject)
                {
                    Debug.Print(ws2["Name"].StringValue);
                }
            }
        }
    

    Thanks for your help in getting to this Xin