Jump to content

Good starting point for WMI programming?


Recommended Posts

I am working on a project that I want to be able to read and use info from the WMI. I have gotten some info from TechNet and MSDN but it seems to make my head spin. I am also not sure what interface I will use to write the code. It will either be VBScript (with an HTA) or AutoIT or Flash Projector.

So can anyone post me some good WMI guides and tutorials and code examples and such?

Link to comment
Share on other sites


I'd have to say the WMICodeCreator and the MSDN class articles are probably the best way to go. Regardless of vbscript or autoit (although PowerShell is out there too), learning WMI via vbscript+WMICC+MSDN is probably the most straightforward way. WMI is pretty easy if you already know the framework around it (vbscript, AutoIT, PS, .NET, etc) - it's just a structured query language, basically.

Link to comment
Share on other sites

I'm no programmer like these studs but I've learned a bit about WMI by using Samurize and editing config files. It has a WMI interface that I like to tinker with. Nothing serious, but why can't you have some fun while you're at it? :)

Doing this in conjunction with AHK.

Link to comment
Share on other sites

Here is a little code that will show you how to convert the WMI OS Install Date and OS Last Boot Date, to a more readable format.

 Dim Tme :Set Tme = CreateObject("WbemScripting.SWbemDateTime")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2")

Dim Obj
Dim BDate, IDate
For Each Obj In Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem")
Tme.Value = Obj.LastBootUpTime
BDate = Left(MonthName(Tme.Month),3) & ", " & Left(WeekdayName(Weekday(Tme.GetVarDate)),3) & vbTab & Tme.GetVarDate
Tme.Value = Obj.InstallDate
IDate = Left(MonthName(Tme.Month),3) & ", " & Left(WeekdayName(Weekday(Tme.GetVarDate)),3) & vbTab & Tme.GetVarDate
Next
MsgBox _
"Installed Date" & vbTab & IDate & vbCrLf & _
"Last Boot Date" & vbTab & BDate,4128,"Wmi Time Demo"

Link to comment
Share on other sites

Okay I got something reasonably working now from some examples, except for a few points:

1) I'm not a complete expert on COM by any means, but I'm finding it hard to procedurize things, because I'm getting an access violation. I find this happens when I try to make all my statements I use to connect to WMI into a function (eg. ConnectWMI(bla, bla, bla); ). Any ideas? This seems to be related to my attempts to clean things up. Remove that, and no crashes. Still need to clean things up to prevent memory leaks, though, so any ideas on this one would be good.

2) Error checking? I looked over the WbemObjectSet object and I'm not seeing anything jump out at me (nothing new, most of this stuff isn't documented too well - I'm fighting the WMP player object on that for another project) in that category. Usually when I enter a statement with an error in it, it just hangs there for a while and then comes back to prompt, but it would be nice to be able to come back with "Incorrect Table Name", or some such thing. I realize I could check the count if it's = 0 (and I do that), but I'm wondering if anything nicer could be done easily enough.

3) Then one thing I'm not sure of is this... I'm guessing from what I've read that there are such things as WMI methods as well? I'm not seeing anything particularly interesting jump out to play with (maybe something in Win32_BaseService?). Any ideas on this one? Edit: It's looking like Win32_Process.Create run against NOTEPAD.EXE ought to do it for this one...should be fine here

I will say once you get the interface returning data, the WMI part is a breeze if you have the documentation and know SQL. :thumbup

Edited by Glenn9999
Link to comment
Share on other sites

1) I'm not a complete expert on COM by any means, but I'm finding it hard to procedurize things, because I'm getting an access violation. I find this happens when I try to make all my statements I use to connect to WMI into a function (eg. ConnectWMI(bla, bla, bla); ). Any ideas? This seems to be related to my attempts to clean things up. Remove that, and no crashes. Still need to clean things up to prevent memory leaks, though, so any ideas on this one would be good.
Sample?
2) Error checking? I looked over the WbemObjectSet object and I'm not seeing anything jump out at me (nothing new, most of this stuff isn't documented too well - I'm fighting the WMP player object on that for another project) in that category. Usually when I enter a statement with an error in it, it just hangs there for a while and then comes back to prompt, but it would be nice to be able to come back with "Incorrect Table Name", or some such thing. I realize I could check the count if it's = 0 (and I do that), but I'm wondering if anything nicer could be done easily enough.
What language?
Link to comment
Share on other sites

Sample?

What language?

Trying this with Delphi. Like was said, write the interface and the rest should take care of itself. But the interface is what needs taken care of. I figured out that using the COM related unit does all the COm initializations/uninitializations so I dropped that. Sample of the relevant code for #1:


WbemLocator := nil;
HRes := CoCreateInstance(Class_SWbemLocator, nil, CLSCTX_INPROC_SERVER,
ISWbemLocator, WbemLocator);
if HRes <> 0 then
ShowMessage('Locator instance not created.');
WBEmServices := nil;
WBEmServices := WBEMLocator.ConnectServer('', '\root\CIMV2', '', '', '', '', 0, nil);
if HRes <> 0 then
ShowMessage('Server connection not created.');
HRes := CoSetProxyBlanket(WbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
nil,
wbemAuthenticationLevelCall, wbemImpersonationLevelImpersonate,
nil, EOAC_NONE);

...(then later on when all is done, commenting these out fixes my access violations, but not cleaning this up either )
// WbemServices._Release;
// WbemLocator._Release;

For #2, it's much like the script that got posted in the thread. Submit a query and then parse the results:


ObjectSet := nil;
ObjectSet := WBEmServices.ExecQuery(Edit1.Text, 'WQL',
wbemFlagReturnImmediately,
nil);

I don't see any error checking on the scripts I see posted on here, so it's hard to tell. I don't see anything in the SWbemObjectSet which indicates any error codes, so I'm not really sure that this one might be a possibility.

(FWIW, I can post the exec for this when I get done if it'll help - one of them is a WQL processor, which I'm sure will help in poking around the WQL tables. It works pretty well except for the issues above)

Edited by Glenn9999
Link to comment
Share on other sites

Okay, I figured out that I didn't need to necessarily worry about clean-up anyway with the format of what I'm using, so I won't worry about posting the WQL program I have.

Edit: Replaced the old one with a newer version. I don't know if the remote connect part works, but the rest seems to work. If you want to connect to the local machine, all you need to do is hit the "Connect" button with nothing in the machine info fields.

Edited by Glenn9999
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...