Top 6 things you need to know when scripting with SCCM
PowerShell and Configmgr 2007/2012:
If you are searching to script with SCCM 2012 post sp1 edition, you must probably already know that there are tons of cmdlets available. Prior to SP1, there were no ‘official’ cmdlets available in order to automate different tasks on ConfigMgr 2012. That is actually the main reason why I started to develop my own PowerShell cmdlets and regroup them in a SCCM2012-Commands module. If you are attempting to script something with SCCM 2012 after version SP1, you might be lucky, and have already everything right there. Simply load the console and then write the following command :
get-command -module configurationManager
You will have the list of all the cmdlets available. Simply filter on the name to find what you are looking for.
[important]The complete list of PowerShell cmdlets avaiable in Configuration manager is available right here
The SP1 brought a huge change to this; It brought A LOT of cmdlets !. Super happy to be, finally, able to automate things in configMgr 2012. It works great ! Except for a few cmdlets where actually we have to admit, a few bugs have been reported. These bugs are reported directly to Microsoft, and are correct (or not) generaly in the next cumulative update package.
You have an issue with a cmdlet, and think it is a bug, verify if somone else has the same issue on the Interet, and especially, check out the “Microsoft Connect” web page. It contains all the current open points concerning issues and feature requests.
[important]Be sure to check out the feedback
page and vote for the new features or bug corrections you would like to see ![/important]
Non-Microsoft Sccm Modules:
a) SCCM 2012
If you know me or follow my blog, you know I am vervant PowerShell scripter, and, in 2014, I actually don’t see WHY you would still use technologies like VBS instead of PowerShell. So one thing you need when scripting with PowerShell, are Cmdlets. For SCCM 2012, a bunch of ‘official’ Cmdlets are available. But, they are present only since the SP1. Before that ? well, the community wrote their own cmdlets based on WMI. My version of the Sccm 2012 module (Sccm2012-commands.psm1) was published on my blog, but I stop the developpement as soon as I heard that the SP1 of SCCM 2012 would bring a complete set of new Cmdlets.
[important]If you are curious, my module is available here: Sccm2012-commands.psm1
b) SCCM 2007
If you need to automate tasks on System Center Configuration Manager 2007, you will most probably know that Microsoft never officially released any PowerShell cmdlets for this version. Fortunatley enough, the internet is huge (again) and the community helps the community. You can use the version 2.0 of the SCCM 2007 commands.
For the older ones of us, the ones that worked with SCCM 2007, no official cmdlets are available. So how are you going to automate your tasks then ? no panic, there is a Powershell module that exists out there. It has been on the web for quite a while.
I have been using it quite a lot throughout different projects. I have written around 30 new functions to original one that was posted on “snowland.se” and written by “Michael Niehaus” and updated by “Rikard Ronkvist” (@riro).
[important]Download the Sccm2007-commands.psm1 cmdlets from here
So if what you are attending to do, is not available in either of those modules, there is one solution left; creating your own cmdlet’s, with WMI.
I will mainly focus in this post on how to create your own PowerShell cmdlets based on WMI, with a big focus on automating products of the System center family.
Creating you own Cmdlets with WMI
0) Get to know WMI:
If you are not already familiar with WMI (or CIM) this is mostl likley where you will need to start. Ineed, scripting with SCCM, or at least, while attempting to create new Cmdlets for SCCM ( or any product of the System center suite), you will have to rely on WMI. In some rare occasions you might need to use COM objects, or .Net functions, but everything concerning the management of SCCM, reside in it’s open WMI classes.
So if you ave never heard of any of this you can take a look here WMI and here CIM. But no worries, we will get there pretty quickly.
When you are writing your new cmdlet, I recommend that follow (more or less) the following methodology in order to avoid to input to much effort in some tasks that did not needed to be done:
- Identify your need (not to difficult right ?)
- Search for some already existing cmdlets
- If there is no “official cmdlet” available, is there one provided by the community ?
- Identify which WMI class you need, and read about it on technet.
- Write and test your own cmdlet.
- Optional: Share your work with the community, especially if you used already existing material from the community 😉
Indeed, why would you invest a lot of time (and who talks about time talks about money right ?) in re-writing things that are already existing ? The internet is huge, and we most oftenly only limit our researches on the first 2 pages of google, and then we go on. Check these things even before opening your PowerShell prompt:
2)MSDN is your best friend:
MSDN is the place where you can find all the online documentation concerning Microsft’s different products. Once you know that there aren’t any cmdlets available for th specefic actions you try to automate, you will need to have an idea on which WMI class you will need to work with. (If not, you can use your favorite search engine, and try tp type things like “SMS_<product>” –> so for software updates try this “SMS_Softwareupdates“. Google general corrects your query to somthing similar that exists, and will often give your question satisfaction.
[important]Once you know the WMI class, search for it prefixing it with MSDN –> MSDN SMS_SoftwareUpdate
Our research brought us right to the help page of the Software Updates WMI class. You will be able to find a list of methods on the left of the screen. All the properties and their type can be found at the bottom of the page. Each of the property will have a small explanation on what the property stands for, and what type of data it is expecting.
Be also sure that you are searching in the good WMI class.
[important]Since WMI classes for clients and servers exists be sure to look in the good WMI class (Server / client class) [/important]
3) The SDK is also your best friend:
The SDK stands for “software developement kit”. It is a documentation that contains samples and documentation in either a downloadable CHM file, or directly online, which contains gold worth of information on how to programatically use SCCM 2007 / 2012. Download the SDK for SCCM 2012 here –> Download (Online version is here –> Check ) Download the SDK for SCCM 2007 here –> Download (Online version is here –> Check )
4) You are going to have a lot of friends, because technet is ALSO going to be your best friend:
Sometimes, you migt get blocked while coding. A WMI error that is just not comprehensible enough, you have done it right, but it still doesn’t work, I need help ! Well, for all these cases, you can “delegate” this to the tecnet forums.
Indeed, the Technet forum is a wonderfull place where you can ask for help. Simply login on te social technet by clicking here, specify a forum where to search in (Arrow on the right), then search for your specefic question by typing in the “searc forum questions” section.
[important]If you are developping new cmdlets, be sure to search (at least) in the “Configuration manager 2012 SDK
Still couldn’t find a solution to you problem ? I actually noticed while searching for a solution to common WMI problems that my results returned nothing. I then decided to post a question in the forum directly, by simply clicking the “Ask a question” button.
Very very often, while i was writting the question on the question page, the webstie would popup similir questions to me and suggest me to read them first. And actually, I had more luck to find results here, then in the normal search option that i mentionned earlier.
[important]Remember, if the search box didn’t get the answer you were searching for, try to write a question. You might be able to find related questions already answerd while typing your question.[/important]
And if that still doesn’t give you a good solution to your problem ? well, simply write a question following the method above. The forum is very very fast, and you will have an aswer very quickly.
5) WMI explorer
Or should I say “WMI ExplorerS” ? There are a few WMI tools that make your life easier. Here you have a short list of the ones I have used and tested. Unfortunatley, they are almost ALL have the same name “WMI explorer”, so differienciating them is not really that straitghforward. Sapien WMI explorer : If you ask me, I would say that it is simply the best : Fast, clean, and precise, It is my prefferend one. But, unfortunatley, it is not free :/ (But it costs only 40$). Get the demo version here. Coretech WMI explorer (written again by Jairdo karivens) get it here.
[important]If you have to choose for one, and you are not 40$ short, go for the sapien
one, it is simply the best ! [/important]
For a deeper analsys of the different “WMI explorer’s” you can visit this article written by Jeffrey Hicks right here –> Link .
6) Troubleshooting WMI errors:
As a scripter, one of our main work methodology is a “Script – Test – Correct” process. This means, that if your function does not work as expected the first time (tested in a lab environment of course ! ) you will troubleshoot the error by looking deeper into the error code returned, on by going through the various log files that are available. Yes, this is how it “should” work, BUT, unfortunatley, for WMI, the return messages are generaly poor. VERY poor ! But, Luckuly for us, fellow powershell + SCCM scripters, I have found a way where you can identify exactly why your function is not working. And I actually already blogged about it.
[important]Read more on how to troubleshoot WMI errors by clicking this link –> here
7) Searching for Collection Members and facing slow results ?
When searching for Colleciton members you can might be confronted to latency. Especially if your collection contains a lot of members. Use the following WMI class in order transform a slow and long search, into a super fast one: SMS_Col_Res_$CollectionId (Of course, $CollectionID contains the ID of your collection ;)) you can find more information here on msdn: http://msdn.microsoft.com/en-us/library/cc145442.aspx
7?? It said top 6 ! Yes, I know, the 7th is a “Bonus” one 😉