Geeks With Blogs
El Grego BizTalk blog
I’ve seen numerous handy WMI scripts and code samples from fellow BizTalk bloggers and from the BizTalk help. Amazingly every one of these samples use late-bound objects calls to invoke methods and access properties from the BizTalk management objects. This makes the code rather verbose, it’s very inconvenient and also very error prone. There's a better way to write your WMI code. Therefore you have to download first and then install the 'Management (WMI) Extensions for Visual Studio .NET 2003 Server Explorer'. This tool makes WMI programming much easier and enjoyable (at least for me). It's no rocket science or anything but it comes in very handy for making your first steps on the WMI moon. You don’t have to take my word on it, look for yourself:
 
After having installed the extension you can go into your Visual Studio’s Server Explorer and select the namespace you want to add management classes from (select ‘add classes’ from the context menu).
 
 
After selecting the desired classes from your namespace (‘root \MicrosoftBizTalkServer’ I assume) the Server Explorer window will contain a list of the selected management object-types. It also shows a list of all instances for a given class when you click it. When having selected an instance, all of its properties will be shown inside the property-window (you could modify them from there too, but I wouldn’t recommend doing it this way).
 
 
Also methods can be invoked on classes and instances; this is nice for testing purposes. *No big deal* you are thinking of course; now pay attention:
 
When selecting 'Generate Managed Class' from the menu, the extension will automatically generate a wrapper class and add it to your solution’s startup project (it targets your .net project language).
 
 
The generated classes have strongly typed properties and methods, so you have access to and can invoke by simply using their name (even enum-types are created). You benefit from good old Intellisense again. Behind the covers the generated wrapper classes still use late-bound objects and calls of course, but you don’t have to care at least about this, nor write ugly late binding code. Now you can write clean high-level WMI code a lot faster.
Take a look at this little WMI sample (where I make a call to ‘GetInstances’: this overloaded method is created automatically and it requires only the where-part of the WQL query to return a typed collection of your WMI object-type).
 
 
Try to rewrite the following method (please don’t focus on the language change; it’s purely for educational purposes. I’ve never executed a single line of this code :-)
 
 
Into this:
 
 
Which one do you prefer?
Posted on Wednesday, March 16, 2005 6:00 AM BizTalk - EAI - B2B | Back to top


Comments on this post: BizTalk WMI programming: How to write cleaner WMI code a lot faster

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
At last a beacon to light the gloom of WMI scripting. Great post.
Left by Hugo Rodger-Brown on Mar 17, 2005 12:09 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
Even though VS WMI interface was there for a long time, I haven't seen anyone explaining about it in the Biztalk world. Good Work Grego.
Left by Saravana Kumar on Mar 17, 2005 1:35 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
I enjoyed your article but I think there's another issue here,
that of generic code and reuse.

By passing in different classes and parameters to a generic method, the code is easy to maintain and reuse.

For example in the two separate calls to processMetrics
the below example targets two different WMI classes.
The code can be easily extended to map to any number of
different WMI classes:


'NETFramework_NETCLRLoading
processMetrics(strTarget, _
"Win32_PerfRawData_NETFramework_NETCLRLoading", _
"BytesinLoaderHeap", "Currentappdomains", _
"CurrentAssemblies", "CurrentClassesLoaded", "TotalAppdomains", _
"TotalAssemblies", "TotalNumberofLoadFailures")

'NETFramework_NETCLRRemoting
processMetrics(strTarget, _
"Win32_PerfRawData_NETFramework_NETCLRRemoting", _
"Channels", "ContextBoundClassesLoaded", _
"ContextProxies", "Contexts", _
"TotalRemoteCalls")


Public Sub processMetrics(ByVal strTarget As String, ByVal strClass As String, ByVal ParamArray strArray() As String)
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
Dim I As Integer

Try
Dim strSQL As String = "Select * from " & strClass
Dim oQuery As New System.Management.ObjectQuery(strSQL)

Dim oScope As New System.Management.ManagementScope("\\" & _
strTarget & "\root\cimv2")

Dim oSearcher As New System.Management.ManagementObjectSearcher(oScope, oQuery)

Dim oService As System.Management.ManagementObject

oWrite = oFile.AppendText("C:\WMI\" & strTarget & ".xls")


For Each oService In oSearcher.Get()
For I = 0 To UBound(strArray)
oWrite.WriteLine(strTarget & vbTab & Date.Today & vbTab & TimeOfDay & vbTab & _
strClass & vbTab & oService("Name") & vbTab & _
strArray(I) & vbTab & oService(strArray(I)).ToString)
Next
Next
oWrite.Close()
Catch errProcessMetrics As Exception
MsgBox("Error in processMetrics for class: " & strClass & " with error: " & errProcessMetrics.Message)
End Try
End Sub

Public Sub setTitles(ByVal strTarget As String)
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter

Try
oWrite = oFile.AppendText("C:\BizTalk\" & strTarget & ".xls")
oWrite.WriteLine("Server" & vbTab & "DateInterval" & vbTab & "TimeInterval" & vbTab & _
"MetricCategory" & vbTab & "Name" & vbTab & "MetricName" & vbTab & "MetricValue")
oWrite.Close()
Catch errSetTitles As Exception
MsgBox("Error in setTitles: " & errSetTitles.Message)
End Try
End Sub
Left by Joseph Karpinski on Mar 25, 2005 4:19 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
This is probably some of the coolest stuff I've seen in a while given that you don't have to have a local biztalk install (or at least there aren't any Biztalk assembly references). What I can't seem to figure out is how to make the generated code work remotely, like from a XP desktop or laptop. Any suggestions on this.
Left by Eric Williams on Jan 28, 2006 3:32 AM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
No one can make it work remotely. Late binding is the only powerful way. Late binding can work remotely. Your example can not.
Left by XtremeCoder on Jan 29, 2006 11:08 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
I have installed the WMI Extension for VS.NET 2003, but the Generate Managed Class menu is always disabled. Is there something I need to do to enable it?

thanks,
craig
Left by Craig Neuwirt on Mar 03, 2006 9:24 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
Never mind the last post, I didn't have a startup project open. cool stuff!
Left by Craig Neuwirt on Mar 03, 2006 9:30 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
You can make it remote. You can adjust generated code as good as hand-written. Adjust this:
private static string CreatedWmiNamespace = "ROOT\\MicrosoftBizTalkServer"; --> ..= @"\\machine\ROOT\MicrosoftBizTalkServer";
Left by Gerard M on Jul 25, 2006 1:35 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
anyone know of a definitive listing of WMI events generated by Biztalk 2006
Left by Linus Joseph on Sep 01, 2006 4:20 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
Linus, take a look at the MOM pack for BTS. it has a list of events that BTS emits and that MOM listens for. That may help

cheers,
benjy
Left by Benjy on Nov 02, 2006 11:54 AM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
any version available for 2005 ?
Left by joe on Jan 20, 2007 6:55 PM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
The version is comes already with product of VS.NET 2005
Left by Suleiman on Oct 23, 2008 8:20 AM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
I am using VS 2005. I have added the classes for BizTalk, but "Generate Managed Classes" options is de-highlighted. Am I missing any setting?
Plz help me with this.
Left by Ajeet Kumar on May 15, 2009 8:56 AM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
Hugo,

Thanks for the article...it's worth mentioning that you have to have a project open in the VS IDE before 'Generate Managed Classes' becomes available.
Left by Simon on May 20, 2009 11:00 AM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
That's also what I told Ajeet in a private mail.
Never had this question in the first 4 years from initial publication :-)
Left by Grego on May 20, 2009 11:10 AM

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster
Requesting Gravatar...
Thank you for posting this. It helped me a lot. I am trying to install printerdrivers through WMI and VB.NET.
Left by Marcus Nyberg on Nov 02, 2009 4:03 AM

Your comment:
 (will show your gravatar)


Copyright © Gregory Van de Wiele | Powered by: GeeksWithBlogs.net