I've recently been working more on Python, since that's what most of the other people I know use as a scripting language, and I figured it would be a good idea to brush up on it a little. As a Windows guy, that meant I would need to dig into the Windows internals, and put together a few tools based on that.
However, I quickly found that the information about Python and OLE is pretty lacking, especially if I don't want to just download a WMI wrapper for it. Don't get me wrong, I'm sure this is really good, but I've never been a fan of working with WMI through a wrapper like that, especially since there is so much more cool stuff you can do with OLE.
So, this should serve as a quick run-down of how to get started with the Windows OLE in Python, specifically WMI. The first thing I found on it was this, which seems to be the tried and true work on Python and OLE, but it is a little dated (Windows 2000? Really?). Likewise, there's a book on Python and Windows out there, but its pretty old too, and I'm not quite sure how much I trust either of their data when they're working on a version of Windows that's over 10 years old at this point. Admittedly the OLE and WMI hasn't changed all that much in that time, but I'd rather have something a little more updated.
First off, you'll need to go get ActivePython. Yes, its the same company that puts out ActivePerl and all the other Active. They might not be the best there is, but they've put a lot of work into making stuff work on Windows, which is exactly what we need in this case. Luckily, they have the pywin32 (also called WIN32all) pre-installed, so we don't have to worry about that. Unfortunately, I couldn't find any good documentation on how to use pywin32; in fact, the documentation I could find involved tons of broken links.
That's where the samples come in. Like a lot of good packages, pywin32 comes with a good chunk of example scripts to do various things. If you installed ActivePython 2.7 to the default directory, look in C:\Python27\Lib\site-packages. For our case, we're specifically interested in the win32com module, which has some tests in C:\Python27\Lib\site-packages\win32com\test which work as great samples. In any case, here are the basics you need to know about using win32com to connect to WMI:
> import win32com.client
We're only interested in the win32com.client module, which does the heavy lifting we need for connecting to WMI.
> wmi = win32com.client.GetObject("winmgmts:\\\\localhost").InstancesOf("win32_process")
The customary example, when talking about WMI, is to demonstrate how to enumerate running processes, so why should I do any differently. You'll notice that win32com.client.GetObject is connecting to winmgmts, the WMI OLE object. Also not that we are connecting to localhost, you can omit everything after the ':' if you like, and are only running locally, but you can connect to a remote computer if you have permissions. There are also options for specifying credentials, but we won't go into that right now. After that, we're creating an enumerator of all the instances of "WIN32_Process", a WMI class.
> for w32 in wmi:
> print(w32.name)
By looping through all the WMI objects, we can print out the names of all the running processes.
Surprise! That's all there is to getting into WMI in Python (assuming you're on Windows). Make sure to take a look at the Microsoft WMI Class Reference so you actually know what you're looking at.