You are on page 1of 9

Retrieving Networking Configuration

Information Using Visual Basic.NET and


VBScript
(Page 1 of 6 )

This article explains how to retrieve “Network settings” information dynamically using Visual
Basic.NET and VBScript.

A downloadable file for this article is available here.

The sample downloadable solution (zip) is entirely developed using Visual Studio.NET 2003
Enterprise Architect on Windows Server 2003 Standard Edition. But, I am confident that it
would work with other versions of Windows (which support .NET 1.1) as well.
I contributed several articles on WMI with VB.NET and VBScript previously(including a
number that cover introductory or basic topics of WMI). I even contributed a series (of about six
articles) on “WMI Programming on VB.NET” covering several aspects of WMI. I strongly
suggest that you go through the series before going through this article.
How to list all IP addresses of a computer using Visual Basic.NET – preparing the wrapper
Before getting the information of “IPAddresses”, we need to create wrapper to store the
“IPAddress” information. Let us proceed with creating a wrapper:
Public Function getNetworkInfoStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("DHCPEnabled"))
dt.Columns.Add(New DataColumn("DHCPServer"))
dt.Columns.Add(New DataColumn("DNSDomain"))
dt.Columns.Add(New DataColumn("DNSHostName"))
dt.Columns.Add(New DataColumn("IPAddress"))
Return dt
End Function
The following method “addNetworkInfo” adds a single row based on the structure you create for
the data table using the above method.
Public Sub addNetworkInfo(ByRef dt As DataTable, ByVal Caption As String,
ByVal Description As String, ByVal DHCPEnabled As String, ByVal DHCPServer As
String, ByVal DNSDomain As String, ByVal DNSHostName As String, ByVal
IPAddress As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("Description") = Description
dr("DHCPEnabled") = DHCPEnabled
dr("DHCPServer") = DHCPServer
dr("DNSDomain") = DNSDomain
dr("DNSHostName") = DNSHostName
dr("IPAddress") = IPAddress
dt.Rows.Add(dr)
End Sub
At the moment, I didn’t give the subnet for the IP Address to be listed. But you can have it
through “IPSubmit”. You just need to add that as part of the structure to get it listed.

Retrieving Networking Configuration


Information Using Visual Basic.NET and
VBScript - How to List all IPAddresses of a
computer using Visual Basic.NET – the code
(Page 2 of 6 )

Once you complete the creation of the wrapper (as specified in the previous section), proceed
with the following VB.NET code to list all the details of the network configuration information
available on your system.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_NetworkAdapterConfiguration
WHERE IPEnabled = True")
Dim dt As DataTable = globals.getNetworkInfoStructure
For Each queryObj As ManagementObject In searcher.Get()
Dim IPAddress As String = ""
If queryObj("IPAddress") Is Nothing Then
IPAddress = queryObj("IPAddress")
Else
Dim arrIPAddress As String()
arrIPAddress = queryObj("IPAddress")
For Each arrValue As String In arrIPAddress
IPAddress &= arrValue
Next
End If
globals.addNetworkInfo(dt, queryObj("Caption"),
queryObj("Description"), queryObj("DHCPEnabled"), queryObj("DHCPServer"),
queryObj("DNSDomain"), queryObj("DNSHostName"), IPAddress)
Next
Me.DataGrid1.DataSource = dt

Catch err As ManagementException


MessageBox.Show("An error occurred while querying for WMI data:
" & err.Message)
End Try
You can achieve the same thing with VBScript as follows:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE
IPEnabled = True",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
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
Next

Retrieving Networking Configuration


Information Using Visual Basic.NET and
VBScript - How to retrieve MAC address and
other miscellaneous network information
using Visual Basic.NET – preparing the
wrapper
(Page 3 of 6 )
In the previous section, I tried to retrieve only basic IP related information. But, if we need to go
into the depth of the IP configuration, we still need to delve into a few more important
properties. Before starting with the code, we need to have a separate wrapper for this concept
again. The wrapper would look something like this:
Public Function getNetworkConfigStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("IPFilterSecurityEnabled"))
dt.Columns.Add(New DataColumn("IPPortSecurityEnabled"))
dt.Columns.Add(New DataColumn("IPXAddress"))
dt.Columns.Add(New DataColumn("IPXEnabled"))
dt.Columns.Add(New DataColumn("IPXNetworkNumber"))
dt.Columns.Add(New DataColumn("MACAddress"))
dt.Columns.Add(New DataColumn("WINSPrimaryServer"))
dt.Columns.Add(New DataColumn("WINSSecondaryServer"))
Return dt
End Function
The following method “addNetworkconfig” adds a single row based on the structure you create
for the data table using the above method.
Public Sub addNetworkConfig(ByRef dt As DataTable, ByVal Caption As
String, ByVal IPFilterSecurityEnabled As String, ByVal IPPortSecurityEnabled
As String, ByVal IPXAddress As String, ByVal IPXEnabled As String, ByVal
IPXNetworkNumber As String, ByVal MACAddress As String, ByVal
WINSPrimaryServer As String, ByVal WINSSecondaryServer As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("IPFilterSecurityEnabled") = IPFilterSecurityEnabled
dr("IPPortSecurityEnabled") = IPPortSecurityEnabled
dr("IPXAddress") = IPXAddress
dr("IPXEnabled") = IPXEnabled
dr("IPXNetworkNumber") = IPXNetworkNumber
dr("MACAddress") = MACAddress
dr("WINSPrimaryServer") = WINSPrimaryServer
dr("WINSSecondaryServer") = WINSSecondaryServer
dt.Rows.Add(dr)
End Sub

Retrieving Networking Configuration


Information Using Visual Basic.NET and
VBScript - How to retrieve MAC address and
other miscellaneous network information
using Visual Basic.NET – the code
(Page 4 of 6 )

Once you complete the creation of the wrapper (as specified in the previous section), proceed
with the following VB.NET code to list all the in-depth details of network configuration
information available on your system.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM
Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
Dim dt As DataTable = globals.getNetworkConfigStructure
For Each queryObj As ManagementObject In searcher.Get()
Dim IPXNetworkNumber As String = ""
If queryObj("IPXNetworkNumber") Is Nothing Then
IPXNetworkNumber = queryObj("IPXNetworkNumber")
Else
Dim arrIPXNetworkNumber As String()
arrIPXNetworkNumber = queryObj("IPXNetworkNumber")
For Each arrValue As String In arrIPXNetworkNumber
IPXNetworkNumber &= arrValue
Next
End If
globals.addNetworkConfig(dt, queryObj("Caption"),
queryObj("IPFilterSecurityEnabled"), queryObj("IPPortSecurityEnabled"),
queryObj("IPXAddress"), queryObj("IPXEnabled"), IPXNetworkNumber,
queryObj("MACAddress"), queryObj("WINSPrimaryServer"),
queryObj("WINSSecondaryServer"))
Next
Me.DataGrid1.DataSource = dt
Catch err As Exception
MessageBox.Show("An error occurred while querying for WMI data:
" & err.Message)
End Try
You can achieve the same thing with VBScript as follows:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE
IPEnabled = True",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "IPFilterSecurityEnabled: " &
objItem.IPFilterSecurityEnabled
Wscript.Echo "IPPortSecurityEnabled: " &
objItem.IPPortSecurityEnabled
Wscript.Echo "IPXAddress: " & objItem.IPXAddress
Wscript.Echo "IPXEnabled: " & objItem.IPXEnabled
If isNull(objItem.IPXNetworkNumber) Then
Wscript.Echo "IPXNetworkNumber: "
Else
Wscript.Echo "IPXNetworkNumber: " &
Join(objItem.IPXNetworkNumber, ",")
End If
Wscript.Echo "MACAddress: " & objItem.MACAddress
Wscript.Echo "WINSPrimaryServer: " &
objItem.WINSPrimaryServer
Wscript.Echo "WINSSecondaryServer: " &
objItem.WINSSecondaryServer
Next

Retrieving Networking Configuration


Information Using Visual Basic.NET and
VBScript - How to retrieve Network Client
Information using Visual Basic.NET
(Page 5 of 6 )

Before getting the information on “Network Clients” installed on your computer, we need to
create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getNetworkClientStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("InstallDate"))
dt.Columns.Add(New DataColumn("Manufacturer"))
dt.Columns.Add(New DataColumn("Name"))
Return dt
End Function
The following method “addNetworkClient” adds a single row based on the structure you create
for the data table using the above method.
Public Sub addNetworkClient(ByRef dt As DataTable, ByVal Caption As
String, ByVal Description As String, ByVal InstallDate As String, ByVal
Manufacturer As String, ByVal Name As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("Description") = Description
dr("InstallDate") = InstallDate
dr("Manufacturer") = Manufacturer
dr("Name") = Name
dt.Rows.Add(dr)
End Sub
Once you complete the creation of wrapper, the following VB.NET code should support it with
some minimum information of “NetworkClients” available on your system.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_NetworkClient")
Dim dt As DataTable = globals.getNetworkClientStructure
For Each queryObj As ManagementObject In searcher.Get()
globals.addNetworkClient(dt, queryObj("Caption"), queryObj("Description"),
queryObj("InstallDate"), queryObj("Manufacturer"), queryObj("Name"))
Next
Me.DataGrid1.DataSource = dt
Catch err As Exception
MessageBox.Show("An error occurred while querying for WMI data:
" & err.Message)
End Try

End Sub
You can achieve the same thing with VBScript as follows:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkClient",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "InstallDate: " & objItem.InstallDate
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "Name: " & objItem.Name
Next
Retrieving Networking Configuration
Information Using Visual Basic.NET and
VBScript - How to retrieve Win Proxy
Information using Visual Basic.NET
(Page 6 of 6 )

Before getting the information on “Win Proxy” installed/configured on your computer, we need
to create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getProxyStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("ProxyPortNumber"))
dt.Columns.Add(New DataColumn("ProxyServer"))
dt.Columns.Add(New DataColumn("ServerName"))
Return dt
End Function
The following method “addProxy” adds a single row based on the structure you create for the
data table using the above method.
Public Sub addProxy(ByRef dt As DataTable, ByVal Description As String,
ByVal ProxyPortNumber As String, ByVal ProxyServer As String, ByVal ServerName
As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Description") = Description
dr("ProxyPortNumber") = ProxyPortNumber
dr("ProxyServer") = ProxyServer
dr("ServerName") = ServerName
dt.Rows.Add(dr)
End Sub
Once you have created the wrapper, the following VB.NET code should support it with some
minimum information of “WinProxies” available on your system.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_Proxy")
Dim dt As DataTable = globals.getNetworkClientStructure
For Each queryObj As ManagementObject In searcher.Get()
Console.WriteLine("Description: {0}",
queryObj("Description"))
Console.WriteLine("ProxyPortNumber: {0}",
queryObj("ProxyPortNumber"))
Console.WriteLine("ProxyServer: {0}",
queryObj("ProxyServer"))
Console.WriteLine("ServerName: {0}",
queryObj("ServerName"))
Next
Me.DataGrid1.DataSource = dt
Catch err As Exception
MessageBox.Show("An error occurred while querying for WMI data:
" & err.Message)
End Try
End Sub
You can achieve the same thing with VBScript as follows:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Proxy",,48)
For Each objItem in colItems
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "ProxyPortNumber: " & objItem.ProxyPortNumber
Wscript.Echo "ProxyServer: " & objItem.ProxyServer
Wscript.Echo "ServerName: " & objItem.ServerName
Next
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at
jag_chat@yahoo.com.

You might also like