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 ...select 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 powershell.org
  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 page...click 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 :
SMS_Collection
SMS_CollectionRule
SMS_CollectionRuleDirect
SMS_CollectionRuleQuery

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.


Monday, October 14, 2013

SCCM 2007 + PowerShell Automation - The Problem with Query Based Deployments


Few months back, I made a resolution of automating the deployment process used in our Project,
Scenario
So if a User needs applications in their machine , they raise a request for the same. After the approval process it comes to our team and we handle the deployment of these apps to the User machines using SCCM 2007 (or ConfigMgr).

So this is how we do it ....we have the QueryRules for the Application collection in place we just go there and add the machine name there manually in the Query Rule for the Application Collection. 

To go with the post I have a test collection in place by the name "Deepak Test 1"...So first I will be showing how we do this manually and then later on how I did it using PowerShell. There something called "Click Count" which is a rough count of how many mouse clicks am using to do these actions in ConfigMgr MMC Console (rough estimate)
Click Count = 1

Manual Way
Step 1. Right  Click and open the properties for the collection. On the "Membership Rules" tab, we have a QueryRule named "test query" for this demo.
Click Count = 2
Step 2. Double-Click the  QueryRule and then "Edit Query Statement"
Click Count = 3
Step 3. Now on the "Query Rule Properties" , you can add a machine in 2 ways either by clicking on the "Show Query Language" and then adding the machine name to QueryExpression (this one is not preferred) or click on the "Criteria" tab (which am gonna do )
Click Count = 4
Step 4: On the next prompt double-click the Query or click on the icon highlighted button to edit the query.
Click Count = 5
Step 5 : Now on the next window put the new machine names under Value to Add and click "Add" or click on any machine names under "Value to Match" to remove (in case application needs to be removed from the machine)
Click Count = 6
And then add other 4-clicks to click on "Ok" buttons 
So total Click Count = 10 (rough count) ...after adding the machine name we have to update the collection (1 more click) and then hit F5 to refresh the view.
See to do a normal task of adding/removing a machine name we had to perform 10-clicks or more..Now on a regular day basis we get like hundreds of these tickets (coz they started migrating VDIs to Win 7...Yep now they are doing it finally).

P.S. - WMI is the answer to managing/automating the ConfigMgr 07 deployments.