BizTalk WMI programming: How to write cleaner WMI code a lot faster

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?

Print | posted on Wednesday, March 16, 2005 6:00 AM

Feedback

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Hugo Rodger-Brown at 3/17/2005 12:09 PM
Gravatar At last a beacon to light the gloom of WMI scripting. Great post.

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Saravana Kumar at 3/17/2005 1:35 PM
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.

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Joseph Karpinski at 3/25/2005 4:19 PM
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

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Eric Williams at 1/28/2006 3:32 AM
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.

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by XtremeCoder at 1/29/2006 11:08 PM
Gravatar No one can make it work remotely. Late binding is the only powerful way. Late binding can work remotely. Your example can not.

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Craig Neuwirt at 3/3/2006 9:24 PM
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

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Craig Neuwirt at 3/3/2006 9:30 PM
Gravatar Never mind the last post, I didn't have a startup project open. cool stuff!

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Gerard M at 7/25/2006 1:35 PM
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";

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Linus Joseph at 9/1/2006 4:20 PM
Gravatar anyone know of a definitive listing of WMI events generated by Biztalk 2006

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Benjy at 11/2/2006 11:54 AM
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

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by joe at 1/20/2007 6:55 PM
Gravatar any version available for 2005 ?

# re: BizTalk WMI programming: How to write cleaner WMI code a lot faster

Left by Suleiman at 10/23/2008 8:20 AM
Gravatar The version is comes already with product of VS.NET 2005

Your comment:





 
Please add 1 and 1 and type the answer here:

Copyright © Gregory Van de Wiele

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski