Wednesday, December 25, 2013

SCCM 2007 + PowerShell - WMI is the key to automation Part 2

In Previous post I talked about how to explore the WMI namespace for ConfigMgr 07.
In this post would like to point out on how to use the SCCM-Commands PowerShell Module , this Module leverages WMI with PowerShell.

Many thanks to Michael Niehaus, Rikard Ronnkvist, Stefan Ringler and Ricardo Cheing authors of this Module for sharing this.

Download this module and extract it, it contains a single PowerShell Module file .this is a Script Module (SCCM-Commands.psm1). Place this File under your Modules Directory in a folder named same as the module i.e. SCCM-Commands



Now you should be able to see the Script module in you session like below:


Now you have to import the module and then you can list all the Advanced functions shipped with this module using either of the below 2 ways:
 1. Use Get-Command -Module SCCM-Commands
 2. Or Use Get-SCCMCommands function within the module itself.

Now the Functions in the Module are Advanced ones and don't have much documentation on how to use the module......though it is very intuitive.
So first you connect to the ConfigMgr 07 site Server having SMS Provider installed using Function Connect-SCCMServer (Confused on which hostname to supply refer my previous post to get the site System having SMS Provider installed on it).

Store the object returned back by the function for later use in the variable $SCCMServer :


Now the main task at my hand was to automate the Query Membership Rule Creation for a collection. So am going to head in that direction.


Querying Collection

First to get the details of a collection....use Get-SCCMCollection function. Now take a look at the syntax for the function below:

There is a mandatory parameter named -SccmServer which expects an Object as the input, we will pass the Object stored in $SCCMServer as an argument to it . There is an optional parameter by the name -Filter which takes filter in WMI Format.

There is a test collection in my TEST environment named "Deepak Test 1" having CollectionID 'SCM013EA' , Now I can use either the name or collectionid as the filter here.


Using the Name as the filter:

PS C:\> Get-SCCMCollection -SccmServer $SCCMServer -Filter "Name='Deepak Test 1'"


Similarly, we can use the CollectionID as the filter too. Below is how you will do it:

PS C:\> Get-SCCMCollection -SccmServer $SCCMServer -Filter "CollectionID='SCM013EA'" -Verbose

Now pipe the output of the above to Get-Member to see some interesting methods (marked ones) and properties on the Object. 

Now if you see some of these methods are quite interesting (marked above) which will ultimately help in automation of Query Based Deployments.... Keep hanging !



Querying Collection Rules

In the above  2 screenshots there is a property by the name CollectionRules but it is empty. So in order to query the present Collection Rules (Query or Direct) in a Collection we will use another function Get-SCCMCollectionRules . Before that we will store the Collection Object returned above in a variable to later make it easy to reference the properties.


For my test collection I have 3 Query Membership Rules already present...below is a screenshot of it:

collection When I seek the same info from PowerShell it does return be back the Query Rules:


If we select just the RuleName and QueryID then it show there are three Query Rules...which is the correct information:



This is it for this blog.
Well you can do add new Query Rules to a collection using the  Add-SCCMCollectionRule . There is not direct way to modify a Query Expression so we have to delete the Query Rule first modify it and Add it which will be the topic for my next post....