Solved

Query Collection in SCCM for members

Posted on 2014-10-23
3
331 Views
Last Modified: 2014-12-05
Good Morning,

I'm trying to query the SCCM database to get the members of a collection. I'm trying to do this in vb.net. I found a script on Microsofts website but couldn't get it to work. I also know how to connect to the SCCM database but not sure how to grab a collection and then export it to a text file or show it in a VB.net List Box. Here's the code i got from microsoft.

' Set up a connection to the local provider.
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set swbemServices= swbemLocator.ConnectServer(".", "root\sms")
Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")

For Each Location In providerLoc
    If location.ProviderForLocalSite = True Then
        Set swbemServices = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
        Exit For
    End If
Next

Call EnumerateCollectionMembers(swbemServices)

Sub EnumerateCollectionMembers(connection)

' Set required variables.
' Note:  Values must be manually added to the queries below.

    ' The following example shows how to enumerate the members of the All Systems (SMS00001) collection.
    Query1 = "SELECT ResourceID FROM SMS_FullCollectionMembership WHERE CollectionID = 'SMS00001'" 
    
    ' Run query.
    Set ListOfResources1 = connection.ExecQuery(Query1, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    Wscript.Echo " "
    Wscript.Echo "Query: " & Query1
    For Each Resource1 In ListOfResources1     
        Wscript.Echo Resource1.ResourceID   
    Next
    
    ' A slower alternative is to use the SMS_CollectionMember_a association class.
    Query2 = "SELECT ResourceID FROM SMS_CollectionMember_a WHERE CollectionID = 'SMS00001'" 
    
    ' Run query.
    Set ListOfResources2 = connection.ExecQuery(Query2, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    Wscript.Echo " "
    Wscript.Echo "Query: " & Query2
    For Each Resource2 In ListOfResources2     
        Wscript.Echo Resource2.ResourceID   
    Next
    
    ' A further alternative is to query the members by using the actual collection class name specified in the MemberClassName property of SMS_Collection. 
    Query3 = "SELECT ResourceID FROM SMS_CM_Res_Coll_SMS00001" 
    
    ' Run query.
    Set ListOfResources3 = connection.ExecQuery(Query3, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)

    ' The query returns a collection that needs to be enumerated.
    Wscript.Echo " "
    Wscript.Echo "Query: " & Query3
    For Each Resource3 In ListOfResources3     
        Wscript.Echo Resource3.ResourceID   
    Next

End Sub

Open in new window

0
Comment
Question by:AnthonySmithMCP
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Nagendra Pratap Singh earned 500 total points
ID: 40402577
You can export a collection using the console if you want.
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40406263
Thanks but that's not what I'm looking for.
0
 
LVL 1

Author Closing Comment

by:AnthonySmithMCP
ID: 40482902
Tried but no answer
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question