Skip to main content

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.


Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

PowerShell : Trust network share to load modules & ps1

Problem Do you have a central network share, where you store all the scripts or PowerShell modules ? What happens if you try to run the script from a network share ? or if you have scripts (local) which invoke scripts or import PowerShell modules stored on this network share ? Well you would see a security warning like below (Note - I have set execution policy as 'Unrestricted' not 'bypass' here): Run a .ps1 from the network share Well this is a similar warning, which you get when you download scripts from Internet. As the message says run Unblock-File cmdlet to unblock the script and then run it, let's try it.