Monday, March 30, 2015

PowerShell + SCCM : Get Resource Collection Membership

Recently at our PowerShell Bangalore User group, we had fun participating in a one day PowerShell + ConfigMgr Hackathon event. Where we had a bunch of ConfigMgr admins worked on using Azure to deploy a full fledged ConfigMgr Lab, also we had fun interacting with each other.

Below pic was the theme for the event, says it all ;)




My friend Harjit  suggested few ideas for the Hackathon. Below is one of the ideas:

"Script that can tell me which collections a particular system or several Systems belong to"


The Final Script is available @Technet for Download 

  

Credits - There is a post by MVP David O'Brien (link in Resource section) which served as a base for my function. The final Function does the following :
  • Fetches the Collection the Resource (Device/User) is member of.
  • Uses WQL Queries to enhance the performance.
  • For the User resource type, it does a WildCard search  .
Also I took my first shot at writing the Pester tests for the Function. Getting the hang of writing unit tests now.


Usage : 

After dot sourcing the Script, you can invoke the Function to either get the information about a Device or User. 
Get-ResourceCollectionMemberhip -Name dexterposh -ComputerName dexsccm -ResourceType User 

    Name                                          ResourceType                                  CollectionName                               CollectionID                                
    ----                                          ------------                                  --------------                               ------------                                
    dexterposh                                    User                                          TestUserCollection                           DEX00032                                    
    dexterposh                                    User                                          All Users                                    SMS00002                                    
    dexterposh                                    User                                          All Users and User Groups                    SMS00004  


If you want the Collection Membership (Name & CollectionID ) of the User Resource then invoke the function like below:



Get-ResourceCollectionMemberhip -Name dexchef -ComputerName dexsccm  

    Name                                          ResourceType                                  CollectionName                               CollectionID                                
    ----                                          ------------                                  --------------                               ------------                                
    dexchef                                       Device                                        Server2012                                   DEX00038                                    
    dexchef                                       Device                                        Server2008                                   DEX00039                                    
    dexchef                                       Device                                        All Systems                                  SMS00001                                    
    dexchef                                       Device                                        All Desktop and Server Clients               SMSDM003 


Below is the GIF showing the above in action :





Resources :


How to find ConfigMgr Collection Membership of client using PowerShell

http://www.david-obrien.net/2014/01/find-configmgr-collection-membership-client-via-powershell/

How to enumerate members of a collection
https://msdn.microsoft.com/en-us/library/hh949334.aspx

4 comments:

  1. Nice initiative Dexter!

    I really like the fact you put a GIF. The function really works like a charm. Just one question, isn't possible to do something similar with the integrated cmdlets provided by the ConfigMgr team, or are there some underlying reasons for using Get-CimInstance (Like performance).

    Keep on the good work my friend!

    ReplyDelete
    Replies
    1. Thanks Stephane,

      You are spot on as to why use CIM cmdlets for performance. The ConfigMgr Module often spits out WMI properties which we don't want so I took the alternative approach.

      In a Production environment a Resource can be a member of many collections and performance has to be kept in mind.
      As you already know using CIM/WMI one can run this Script from any workstation :)

      Cheers !

      Delete
  2. I forgot to mention, what also very neat is, is that you shipped the pester test with it. I am looking forward to read more about that part ;)

    ReplyDelete
    Replies
    1. Pester is neat and I am getting the hang of it now. Trick is to write tests before you start coding.

      Delete