sccm powershell scripting wmi

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 :

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.[/important]

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[/important]

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.[/important]

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.

1)Basic methodology:

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:

  1. Identify your need (not to difficult right ?)
  2. Search for some already existing cmdlets
  3. If there is no “official cmdlet” available, is there one provided by the community  ?
  4. Identify which WMI class  you need, and read about it on technet.
  5. Write and test your own cmdlet.
  6. 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 [/important]

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.

MSDN SMS_SoftwareUdates

 

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.

Social technet

 

[important]If you are developping new cmdlets, be sure to search (at least) in the “Configuration manager 2012 SDK” forum.[/important]

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.

Social technet configuration manager

 

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.

Social technet configuration manager

 

[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 ![/important]

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  😉

By | 2016-10-19T21:00:17+00:00 July 14th, 2014|ConfigMgr, ConfigMgr 2007, ConfigMgr 2012, Functions, PowerShell, Sticky, WMI|9 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from PowerShell.org. Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

9 Comments

  1. Ramon July 28, 2016 at 9:46 pm - Reply

    Cannot find SCCM module you referred at
    Link http://powershelldistrict.com/sccm-2007/sccm-2007-powershell-module/
    does not contain anything

  2. Tom February 5, 2016 at 10:13 am - Reply

    Hi Stephane, thanks for quick reply on this.

    I asked you this because I have a situation where a custom tool was built by service provider to install PDP role on many devices at once – like 200 PDP or so on WIn Srv 2008.
    I don’t know what code\method was used but I found one machine listed a couple of times as PDP. This is impossible when you use SCCM’s wizard, a message pops up saying that machine is already listed…
    My assumption is that bad code is used (definitely not PS) so I’ll suggest that they switch to PS from now on…

    Thanks again,
    Tom

  3. Tom February 5, 2016 at 9:24 am - Reply

    Do you know if managing (making changes like adding a DP) the SCCM 2012 via WMI is supported by Microsoft? I mean, if something goes wrong, will MS help you or not?

    • Stephane February 5, 2016 at 10:03 am - Reply

      Hi tom. It absolutley is!
      What not supported is though, is making direct changes into the database tables.

  4. Alex January 30, 2015 at 2:21 pm - Reply

    Hi!
    Do you know any way to show informational window for end-user (SCCM 2012 client) using client features (not ps, vbs, hta, etc.)?

    • Stephane January 30, 2015 at 2:29 pm - Reply

      I don’t really understand your question. You are asking if I know how to show a message box on a SCCM 2012 client without using any scripting language. Is that your question? What do you want to show them exactly?

  5. Guido Oliveira November 28, 2014 at 7:06 pm - Reply

    As usual magnifique content!

  6. DexterPOSH July 14, 2014 at 9:26 pm - Reply

    Great Post ! Stephane.
    Keep rocking 🙂

  7. Flavien SALSMANN July 14, 2014 at 6:38 pm - Reply

    Great stuff, nice guide to begin with SCCM Powershell scripting!

Leave a Reply

%d bloggers like this: