Saturday, October 19, 2013

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

Well in the quest of automating ConfigMgr 07.....WMI is the key. Now this post isn't a primer on how to use WMI with PowerShell.
There are very good resources which you can hit like:
  1. Free Ebook by Ravi Sir (PowerShell MVP) - "WMI Query Language via PowerShell" (referred to me by @LaerteSQLDBA :) )
  2. Book by Richard Siddaway (PowerShell MVP) - "PowerShell and WMI"
There are other good resources like HeyScriptingGuy's blog where one can serach for WMI tag, there is whole bunch of information out there on leveraging WMI with PowerShell. If you haven't checked out this yet then you are missing something very cool to manage your Infrastructure.

That being mentioned the journey to exploring WMI starts by connecting to the SCCM Site System which has the SMS provider installed.
Open the ConfigMgr console > Connect to your Central Site/Primary Site > Site Management > Right -click site Properties. The information where SMS provider ins installed can be fetched by site properties general tab in ConfigMgr MMC

After getting the Server having SMS provider installed, we are going to explore the SMS provider a bit for the relevant classes.
To explore WMI you can use many tools like :
  1. WMI CIM Studio (par of WMI Administrative tools from Microsoft)
  2. PowerShell based WMI Explorer created by Marc van Orsouw (aka /\/\O\/\/) hosted at
  3. Sapien WMI Explorer (this needs you to register an account before downloading)
I am going to use WMI CIM Studio for this post on showing how to connect to SMS provider and the find the relevant classes for the Application deployment.
So fire on WMI CIM studio from  Start Menu > All Progams > WMI Tools > WMI CIM Studio

This will essentially open up an IE on "Allow Blocked Content" when it prompts.

Later a pop-up asking which namespace to connect to will be presented. By default it will try to connect to your local machine's Root\CIMv2 namespace.
Click on the below highlighted icon :

After this it will ask you to browse the namespace. In the machine name field fill in the Server having SMS provider installed and in the starting namespace fill in "root\sms\site_<3 letter SiteCode>". Click Connect.

WMI CIM Studio will then prompt you for the login. Check the highlighted box if you want to connect as the current user or supply alternate credentials which have access on ConfigMgr 07 to perform tasks.

After this press "Ok" and wait until WMI CIM studio retrieves the classes definitions from the namespace.
Once done it will loaded with whole lot of SMS WMI classes..... Click on a Class to see the member definitions (Properties, Methods and Associations)

Now as part of the automation quest ....I was set on finding to automate the deployment tasks we do manually now explained here in this post.
In nutshell as part of our deployment activity we manipulate collection by creating Query Membership CollectionRule to which we add a custom query having different machine names , machines which we want to see reflected in the application collection so that the related application advertisements show up on that machine for the end user to install...Pheww !

That being said.....If one doesn't have any idea about which classes to use...then just start with hit and trial approach.
Now I am going to search for classes with keyword collection in it...

Click on the little binocular  icon on the WMI CIM studio.
Then enter "collection" keyword and hit Go button...then it will list all the classes containing the keyword "Collection" in it...Double-click the class to go to its definition. In this case am double-clicking on the class SMS_Collection .

After this we need to explore the SMS_Collection Class definition for the required property or method which will help us in creating QueryRules.
Go to the methods tab for this class.......

If you want to explore the method, just right-click and select Method Qualifiers

Check the Description what it says:
"This method is used to add new rules to the SMS_CollectionRule property. Returns the query ID if the rule was a query rule, 0 otherwise."  <-- Voila

We can later explore  the "Edit Method Parameters" (in above screenshot ) on how to invoke this method.

So I began making a list of interesting classes which help me in achieving the automation task I intended to do.
Few of these which you can explore are :

Well the possibility are endless....if you are trying to manage distribution points then start exploring what WMI has to offer for them.
Similarly, go explore what you can do with packages, advertisements, programs etc.

More on this later.....Well we can always use PowerShell to query the WMI classes and check the members.

But I found this approach to do research better.
Next post I will explore on using already written PowerShell module for SCCM.