Created On:  27 October 2011

Problem:

How do we execute a WMI query from Silktest Workbench (VB.NET)?

Resolution:

One of the main tasks in WMI (Windows Management Instrumentation) is querying WMI for information about computer components and software. For example, you can request that WMI return the name and version of an operating system, or the amount of free disk space on a hard disk. The information that you query is made available through WMI classes that are installed in the WMI repository on a computer.  Each class is a part of a namespace, with each namespace holding similar classes.  For example, the root\CIMV2 namespace contains classes that hold information about the Windows platform and your computer components.

Pre-Requisites:
.NET reference:  System.Management
Located: %WINDIR%\assembly\GAC\System.Management\1.0.5000.0__b03f5f7f11d50a3a\System.Management.dll
 
WMI code creator tool: 
 
Download and extract the WMI code creator tool from the zip file.
Build the query you wish to use in the “WMI code creator tool”.
From the “WMI code creator tool” menu choose “code language” then select “Visual Basic .NET” output.
Click “Open the code in notepad”, then copy it the clipboard, where is ready to be manipulated and adapted to the Silktest workbench executable script.
 
For the purpose here, I will build a query via the “WMI code creator tool” to check the version of internet explorer. See example code below:
 
Imports System
Imports System.Management
Imports System.Windows.Forms
 
Namespace WMISample
 
    Public Class MyWMIQuery
 
        Public Overloads Shared Function Main() As Integer
 
        Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2\Applications\MicrosoftIE", _
                    "SELECT * FROM MicrosoftIE_FileVersion WHERE Version = '8.0.6001.18702'")
 
                For Each queryObj As ManagementObject in searcher.Get()
 
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("MicrosoftIE_FileVersion instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Version: {0}", queryObj("Version"))
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Function
    End Class
End Namespace
 
 
Basically we will need to re-use the code generate within the function, in the example above this code is found between the keywords “Try”.
I have highlighted in Red the code which will be re-used.
 
Below is a Silktest blank script example:
 
Public Module Main
      Dim _desktop As Desktop = Agent.Desktop
 
      Public Sub Main()
            ‘cursor position
      End Sub
End Module
 
 
Below is the merged/adapted code, additionally we also need to add the System.management.dll reference, and to the final script we also need to add the appropriate imports. See example below.
 
Imports System
Imports System.Management
Imports System.Windows.Forms
 
Public Module Main
      Dim _desktop As Desktop = Agent.Desktop
 
      Public Sub Main()
            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2\Applications\MicrosoftIE", _
                    "SELECT * FROM MicrosoftIE_FileVersion WHERE Version = '8.0.6001.18702'")
 
                For Each queryObj As ManagementObject In searcher.Get()
 
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("MicrosoftIE_FileVersion instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Version: {0}", queryObj("Version"))
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
      End Sub
End Module
 
Console.WriteLine CMD’s display to the output view of Silktest workbench, this view can be enabled from the Workbench “View” menu and then selecting “Output”, when the code is executed then such CMD’s are visible in the Silktest output console.