WMI in VB asp.net

Associate
Joined
23 Nov 2002
Posts
1,172
Location
Surrey
Taken from the MSDN website here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_tasks__networking.asp

I can't seem to get any of their samples working because they are incomplete and assuming I have done something that I havent. For example this code below should determine the IP address of the PC. But on my ASPX VB codepage I immediately get alerts that objWMIService, IPConfigSet and IPconfig are not declared. Similar declarations are needed in every one of their examples but I don't know what. Is there a page which tells me the complete code? All those WMI codes would be incredibly useful if only I didn't fall at the first hurdle.

Code:
    visual basic code:strComputer = "."
    Set objWMIService = GetObject( _ 
        "winmgmts:\\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery _
        ("Select IPAddress from Win32_NetworkAdapterConfiguration ")
     
    For Each IPConfig in IPConfigSet
        If Not IsNull(IPConfig.IPAddress) Then 
            For i=LBound(IPConfig.IPAddress) _
                to UBound(IPConfig.IPAddress)
                    WScript.Echo IPConfig.IPAddress(i)
            Next
        End If
    Next
 
Ah yes you are right it is VBScript.

Ideally I like to be able to get hold of code like this that will run in a VB.net executable or a ASP.net code-behind page. It just looked all so well organised on the MSDN site, something like that would be great!
 
To sort this problem I found a tool called the WMI Code Creator v1.0 on the MS VB scripting technet website. The tool lets you browse all the WMI namespaces, classes and properties, and will automatically generate the WMI code for you! It can produce with C#, VB.Net or VBScript code.

Amazingly helpful tool for me! For example, here is the code to get PCs IP Address. This can be easily re-adapted for use within a web form, changing console output to adding to an arraylist databinded to a listbox.

Code:
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", _
					"SELECT * FROM Win32_NetworkAdapterConfiguration") 

				For Each queryObj As ManagementObject in searcher.Get()

					Console.WriteLine("-----------------------------------")
					Console.WriteLine("Win32_NetworkAdapterConfiguration instance")
					Console.WriteLine("-----------------------------------")

					If queryObj("IPAddress") Is Nothing Then
						Console.WriteLine("IPAddress: {0}", queryObj("IPAddress"))
					Else
						Dim arrIPAddress As String()
						arrIPAddress = queryObj("IPAddress")
						For Each arrValue As String In arrIPAddress
							Console.WriteLine("IPAddress: {0}", arrValue)
						Next
					End If

					If queryObj("IPSubnet") Is Nothing Then
						Console.WriteLine("IPSubnet: {0}", queryObj("IPSubnet"))
					Else
						Dim arrIPSubnet As String()
						arrIPSubnet = queryObj("IPSubnet")
						For Each arrValue As String In arrIPSubnet
							Console.WriteLine("IPSubnet: {0}", arrValue)
						Next
					End If
				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
 
Back
Top Bottom