Digging into Network Adapters with Visual Basic.NET and VBScript using WMI - Listing extended information about network adapters using WMI
(Page 3 of 4 )
In the previous section, we only displayed preliminary information. The users may need further extended information like IP address, DHCP information, and so on from network adapters using WMI. Let us see how to get it using VB.NET:
Try
Dim searcher As New ManagementObjectSearcher( _
"rootCIMV2", _
"SELECT * FROM
Win32_NetworkAdapterConfiguration")
For Each queryObj As ManagementObject In searcher.Get
()
Me.TextBox1.Text &= "Caption: " + queryObj
("Caption") & ControlChars.NewLine
Me.TextBox1.Text &= "DHCPEnabled: " + (queryObj
("DHCPEnabled").ToString) & ControlChars.NewLine
Me.TextBox1.Text &= "DHCPServer: " + queryObj
("DHCPServer") & ControlChars.NewLine
Me.TextBox1.Text &= "DNSDomain: " + queryObj
("DNSDomain") & ControlChars.NewLine
Me.TextBox1.Text &= "DNSHostName: " + queryObj
("DNSHostName") & ControlChars.NewLine
If queryObj("IPAddress") Is Nothing Then
Me.TextBox1.Text &= "IPAddress: " + queryObj
("IPAddress") & ControlChars.NewLine
Else
Dim arrIPAddress As String()
arrIPAddress = queryObj("IPAddress")
For Each arrValue As String In arrIPAddress
Me.TextBox1.Text &= "IPAddress: {0}" +
arrValue & ControlChars.NewLine
Next
End If
If queryObj("IPSubnet") Is Nothing Then
Me.TextBox1.Text &= "IPSubnet: " + queryObj
("IPSubnet") & ControlChars.NewLine
Else
Dim arrIPSubnet As String()
arrIPSubnet = queryObj("IPSubnet")
For Each arrValue As String In arrIPSubnet
Me.TextBox1.Text &= "IPSubnet: {0}" +
arrValue & ControlChars.NewLine
Next
End If
Me.TextBox1.Text &= "MACAddress: {0}" + queryObj
("MACAddress") & ControlChars.NewLine
Me.TextBox1.Text &=
"======================================" & ControlChars.NewLine
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for
WMI data: " & err.Message)
End Try
I excluded some of the properties (as it would make the program too long) from the existing WMI class (Win32_NetworkAdapterConfiguration) to give you only the most important ones. The above program would list the Caption, status of DHCP, DHCP Server IP if available, DNS info, IP address of the adapter and so on. You can further extend the above program with several other properties available in the "Win32_NetworkAdapterConfiguration" class. You can even refer to MSDN online for further properties.
You can achieve the same thing with VB Script by using the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer &
"rootCIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "DHCPEnabled: " & objItem.DHCPEnabled
Wscript.Echo "DHCPServer: " & objItem.DHCPServer
Wscript.Echo "DNSDomain: " & objItem.DNSDomain
Wscript.Echo "DNSHostName: " & objItem.DNSHostName
If isNull(objItem.IPAddress) Then
Wscript.Echo "IPAddress: "
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
End If
If isNull(objItem.IPSubnet) Then
Wscript.Echo "IPSubnet: "
Else
Wscript.Echo "IPSubnet: " & Join(objItem.IPSubnet, ",")
End If
Wscript.Echo "MACAddress: " & objItem.MACAddress
Next
Next: Listing network connection information using WMI >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee