Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  WMI connections made easy in VB.NET  »  In Conclusion...
WMI connections made easy in VB.NET
by Martin de Klerk | Published  04/24/2005 | .NET Framework | Rating:
Martin de Klerk

Born in 1958 and bitten by the computer-bug in 1976, I am an autodidact to the letter.

Through the years I have:
  - accumulated programming experience ranging from writing device-drivers to developing advanced MIS programs.
  - mastered languages like Assembler, C/C++/C#, (several flavours of) Basic, Cobol, Forth, Eiffel and Clipper
  - filled nearly every job/position the IT world has to offer as employee or contractor.
  - worked with customers ranging from your local grocery-shop, farmers, hospitals, offshore companies, governemental departments to multinationals like Shell, Afga-Gevaert, ABN/AMRO and Phillips N.V.

Currently I am:
  - running my own consultancy business since 1993.
  - developing 80% of my solutions in VB.NET
  - dividing my sparse moments of free time between my family, music (especially enjoy jamming with my son : on the guitar he's the gifted one, I just manage a bit of semi-decent guitar/harmonica play) and VBCity.
  - eagerly awaiting the official release of .NET Framework 2.0

 

View all articles by Martin de Klerk...
In Conclusion...

When you're new to WMI, I suggest you start with ( aside from a lot of reading up) downloading CIM-Studio and ScriptoMatic 2.0. These two utilities will provide you with an easy way to explore the WMI namespaces, classes and objects. Although script-driven, links are provided at the end of this article. Also download the WMI SDK for the documentation.

As I told you before, this was my first involvement in WMI. As such, I can only conclude that my way of coding works (or not) but I can not vouch for doing it the proper WMI way. WMI delivers a very powerful functionality , but ONLY when all (sub)systems involved (like DCOM, RPC, SMNP, Firewall, Group Policies, etc) are working in perfect harmony. If not working in perfect harmony, you easily find yourself looking for the needle in the proverbial haystack.

I will give you an example: during the development of the printer monitor program, I was forced to poll the printerservers while WMI offers a lot of goodies I'd like to use instead, like the ManagementEventWatcher class. This class triggers an event when a selected event (like the creation of a print job) took place. My intention was to create a service for printerservers which would inform my monitor program on printer-states and printing progress. As WMI contains the possibility to remotely create and start services and processes (so manual installation of my service was not required: no-touch deployment!) this approach would also reduce the network traffic to the minimum and free up resources on the computer running the monitor program. Programmers heaven, one should think.

Alas, after two weeks of struggling with the ManagementEventWatcher class I still got a 'method not supported' exception when I tried to invoke the ManagementEventWatcher.Start() method with VB.NET code. After having scavenged the WMI logfiles (stored in \windows\system32\wbem) , MSDN, forums, newsgroups and other Internet resources on this topic I gave up and concluded that I stumbled upon yet another OS dependency. So I had to revert to the polling approach to retain compatibility with other versions of Windows. Maybe an actual Windows server is needed to make use of this class in VB.NET, I do not know. Now when I'm wrong or overlooking the obvious, I'll happily stand corrected: - emdek@vbcity.com .

  Now, before you start hitting the e-mail button, let me show you the signature of one of VBCity's oldest (membership-wise that is) and esteemed members: CanOz. He selected a quote from George Bernard Shaw, which I find totally spot on:

    I am not a teacher
    only a fellow traveller of whom you asked the way.
    I pointed ahead
    ahead of myself as well as of you.

Say no more....

List of Helpful Links:

Using WMI with the .NET Framework

Accessing Management Information with System.Management

Operating System Availability of WMI Components

WMI Win32 Classes

WMI SDK

WMI Administrative Tools (including CIM studio)

ScriptoMatic 2.0

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.36507936507937 out of 5
 63 people have rated this page
Article Score42880
Related Articles
Attachments
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 07/13/2005)
Rating
This article is great. Easy to read and understand. It gave me everything I needed.
 
Comment #2  (Posted by an unknown user on 07/22/2005)
Rating
This is a good starter. Luckily, you provide the links to the downloads at the end of the article. It's perhaps a good idea to provide them inside the text as well.
 
Comment #3  (Posted by an unknown user on 07/29/2005)
Rating
It is perfect
 
Comment #4  (Posted by an unknown user on 08/02/2005)
Rating
After days of crawling the net, this is the most professional article found yet on WMI & remote connectivity.

WELL DONE !
 
Comment #5  (Posted by an unknown user on 08/02/2005)
Rating
After days of crawling the net, this is the most professional article found yet on WMI & remote connectivity.

WELL DONE !
 
Comment #6  (Posted by an unknown user on 08/24/2005)
Rating
Great approach to introducing people to WMI. Simple to read and understand. Great Job.
 
Comment #7  (Posted by an unknown user on 09/01/2005)
Rating
Found exactly what I needed to fix my code. Thanks
 
Comment #8  (Posted by an unknown user on 10/06/2005)
Rating
This is a great job. In my case help me to resolve a problem with network server monitoring. Thanx for all.
 
Comment #9  (Posted by an unknown user on 10/17/2005)
Rating
Nice and simple.
 
Comment #10  (Posted by an unknown user on 11/12/2005)
Rating
Impeccably presented.
 
Comment #11  (Posted by an unknown user on 12/01/2005)
Rating
It is what I need to use WMI. And the links is useful for me
 
Comment #12  (Posted by an unknown user on 12/02/2005)
Rating
Excellent article. Very well written.
 
Comment #13  (Posted by Andy Bonner (aka Skullcrusher) on 12/04/2005)
Rating
Just found another port that needed to be allowed on XP SP2.

TCP Port 1038 - Message Tracking Query Protocol
 
Comment #14  (Posted by Benjamin Lindelof on 12/09/2005)
Rating
Unable to follow the author.
 
Comment #15  (Posted by an unknown user on 12/09/2005)
Rating
No method to read the Arrays in WMI?
 
Comment #16  (Posted by an unknown user on 12/09/2005)
Rating
No method to read the Arrays in WMI?
 
Comment #17  (Posted by Benjamin Lindelof on 12/09/2005)
Rating
I found the bug. Instead of:
myManagementScope = New System.Management.ManagementScope("\" & _
It should say:
myManagementScope = New System.Management.ManagementScope("\\" & _

 
Comment #18  (Posted by an unknown user on 12/12/2005)
Rating
The article is very good. However, I can't figure out how to download the code. I have searched the following page and can't find the word "download" or anything tat looks like a download link for the ConnectionTester referenced on page 5 of the article:

http://www.devcity.net/Articles/144/6/article.aspx

Thanks!
 
Comment #19  (Posted by an unknown user on 12/12/2005)
Rating
The article is very good. However, I can't figure out how to download the code. I have searched the following page and can't find the word "download" or anything tat looks like a download link for the ConnectionTester referenced on page 5 of the article:

http://www.devcity.net/Articles/144/6/article.aspx

Thanks!
 
Comment #20  (Posted by an unknown user on 12/25/2005)
Rating
Many thanks, you saved me a lot of time (and maybe grey hairs ^^)!
 
Comment #21  (Posted by an unknown user on 12/25/2005)
Rating
To look at additional WMI classes, see the wbemtest.exe tool located on any system with WMI installed. Just goto Start --> Run, then type "wbemtest", then connect to the "root\cimv2" (or a different) namespace, click Enum Classes --> Recursive!
 
Comment #22  (Posted by Bhaskar on 01/20/2006)
Rating
Thanks for the fruitful efforts made !

This code works very well when the servername/IP Address is "localhost"/127.0.0.1

When i give Servername as one of the systems in the LAN it gives:
"Connecting to {0}DevServer{0}DevServer is online with IP address: {1}192.168.1.25.Error while connecting to {0}DevServer: {1}Access denied"

This is the case with all the systems in my LAN.
Please help!!!
 
Comment #23  (Posted by an unknown user on 01/21/2006)
Rating
Either I am blind or there is no link to download this code. I am exited about it, but am also frustrated that I cannot download it :(
 
Comment #24  (Posted by Kevin on 01/29/2006)
Rating
Why is there no remoting example using early binding. All that is said is that mgmtclassgen.exe can be used to make WMI strongly typed.
 
Comment #25  (Posted by an unknown user on 02/02/2006)
Rating
great
 
Comment #26  (Posted by an unknown user on 02/11/2006)
Rating
Well, I was looking for this information and, wow, I found much more than I expected. Great job! Hopefully your grey hair turn back! ;)
 
Comment #27  (Posted by an unknown user on 03/09/2006)
Rating
just easy too read - keep on goin'
 
Comment #28  (Posted by an unknown user on 03/09/2006)
Rating
Excellent tutorial! The download is difficult to find, but not the authors fault. Works great. There is one mistake in the first page example as mentioned already, missing a \. Should be \\. Anyway, great stuff, the class is awesome!
 
Comment #29  (Posted by an unknown user on 03/13/2006)
Rating
I find this article easy to understand, it's perfect for a beginner like me searching to get the best from WMI
 
Comment #30  (Posted by an unknown user on 03/31/2006)
Rating
Felicitaciones.
Es un articulo facil de entender para los desarrolladores de otros idiomas.

Deben cambiar el link de:

WMI Administrative Tools (including CIM studio)

a:


http://www.microsoft.com/downloads/details.aspx?FamilyID=6430f853-1120-48db-8cc5-f2abdc3ed314&DisplayLang=en

Oscar José Lofrano Maturi
ojlofranom@msn.com
MCP VB.NET Windows Application
Venezuela
 
Comment #31  (Posted by Someone on 04/18/2006)
Rating
Great article! But there is a bug in the function ExecWmiQuery ;) It does not work for a remote Win2003 machine that requires login. Instead of

_ManagementSearcher = New System.Management.ManagementObjectSearcher(_ManagementScope.Path.ToString, cWmiQuery) ' bug: this will cause a new ManagementScope object to be created, when the correct ManagementScope object already exists

Use:

Dim query As New ObjectQuery(cWmiQuery)
_ManagementSearcher = New System.Management.ManagementObjectSearcher(_ManagementScope, query)

Your code creates a new instance of ManagementScope and does not set the UserName and Password connection options. Instead use the existing _ManagementScope variable which does have the username and password set already! :)

 
Comment #32  (Posted by an unknown user on 04/20/2006)
Rating
A superb article to newbies of WMI like me but i want more info for remote connections,that code wont work in XP Machines and Server 2003
 
Comment #33  (Posted by an unknown user on 05/09/2006)
Rating
interesting
 
Comment #34  (Posted by Stuart Nathan on 06/27/2006)
Rating
When I run your example, I get the error message "RPC Server is unavailable" when I try to connect to another machine.
 
Comment #35  (Posted by an unknown user on 06/30/2006)
Rating
Very interesting article about the link between scripting & vb.net.Tnx!
 
Comment #36  (Posted by an unknown user on 07/06/2006)
Rating
Excelentemente explicado. De mucha utilidad y muy claro.
 
Comment #37  (Posted by an unknown user on 07/23/2006)
Rating
Very well written article. Easy to follow and informative. Thank you.
 
Comment #38  (Posted by an unknown user on 07/26/2006)
Rating
Easy to read, easy to understand.
Great article for someone starting out!
 
Comment #39  (Posted by sovannborith on 08/17/2006)
Rating
my computer does not have system.Management namespace. how can i get it. please reply

 
Comment #40  (Posted by an unknown user on 08/19/2006)
Rating
Breath of fresh air on the subject. I only wish that my web searches hit on the article sooner.
 
Comment #41  (Posted by an unknown user on 10/05/2006)
Rating
Great Job!! Thanks a lot
 
Comment #42  (Posted by Muhammad Aamir Hassan on 10/11/2006)
Rating
i really enjoyed this snipt. May God help you

aamir
 
Comment #43  (Posted by Lesh Augustus on 11/02/2006)
Rating
Set System Information or Set System Environment Variable in VB.Net
===================================================

Dim objEv As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_Environment")
For Each objMgmt As ManagementObject In objEv.Get
If objMgmt("Name") = "Path" And objMgmt("UserName") = "" Then
Dim strPath As String = objMgmt("VariableValue")
If strPath.ToLower.IndexOf("cvsnt") >= 0 Then
If objMgmt("VariableValue").ToString.Substring(objMgmt("VariableValue").ToString.Length - 1) = ";" Then
objMgmt("VariableValue") = objMgmt("VariableValue") + Trim(txtNewPath.Text) + ";"
Else
objMgmt("VariableValue") = objMgmt("VariableValue") + ";" + Trim(txtNewPath.Text) + ";"
End If
End If
objMgmt.Put()
MessageBox.Show("Path Added Successfully")
End If
Next
 
Comment #44  (Posted by