Part 2: Scripting Basic TCP/IP Networking on Clients

In some kinds of IT environments, such as convention centers, hotels, and universities, network administrators must frequently change network client information because transient users and computers come and go. In these kinds of computing environments, using scripts to configure network clients may prove particularly useful. Even on less turbulent TCP/IP networks, scripting basic configuration on clients can help standardize change and configuration management, avoid manual administrative errors, and accomplish bulk changes on many clients rapidly and effectively.

Windows provides extensive GUI and command-line functionality to help network administrators manage TCP/IP network clients. Through Network and Dial-Up Connections in Windows 2000 and Network Connections in Windows XP and Windows Server 2003 (in Control Panel), you can view and modify most network settings. Widely used command-line tools such as Ipconfig.exe and Ping.exe provide additional options. Sometimes these are all you need to accomplish a networking task. However, if you find yourself clicking OK repeatedly or trying to change a parameter that is contingent on the state of two other parameters on a hundred computers, the techniques detailed in this paper should prove useful.

This section covers the basic techniques for retrieving and changing the most common TCP/IP settings, such as IP addresses and subnet masks. The section begins with a brief review of the non-scripting methods and command-line methods that you can use to perform these tasks. Next, the section covers a variety of scripting techniques for working with the two most important WMI classes for these tasks. Finally, it shows how to retrieve extended TCP/IP settings by using a script that reproduces nearly all the functionality of the ipconfig /all command.

On This Page

Retrieving Basic TCP/IP Client Settings by Using Non-Scripting Methods
Retrieving Basic Settings with a Script
Displaying Expanded IP Configuration Data
Tools for Basic TCP/IP Networking on Clients

Retrieving Basic TCP/IP Client Settings by Using Non-Scripting Methods

To simply check a couple of TCP/IP settings on one computer, many network administrators prefer to use the Windows interface that is provided in Windows XP through Network Connections and in Windows 2000 through Network and Dial-Up Connections.

To check TCP/IP settings by using the Windows interface in Windows XP

  1. Open Control Panel and double-click Network Connections.

  2. In the Network Connections dialog box, right-click a specific network connection and then click Properties.

  3. In the properties dialog box for the network connection, select Internet Protocol (TCP/IP) and then click Properties.

Figure 1 shows the available options when you use the Internet Protocol (TCP/IP) Properties dialog box.

Figure 1   Internet Protocol (TCP/IP) Properties Dialog Box

Figure 1   Internet Protocol (TCP/IP) Properties Dialog Box

The DNS, WINS, and Options tabs also display settings for those technologies and enable you to change most settings.

Getting Basic Settings by Using Ipconfig.exe

Many administrators use command-line tools to obtain TCP/IP client configuration information. One popular command-line tool is Ipconfig.exe, which is included with the Windows operating system. Figure 2 illustrates the network settings you can display by using Ipconfig.exe.

Figure 2   Typical Network Settings Displayed by Ipconfig.exe

Figure 2   Typical Network Settings Displayed by Ipconfig.exe

You can use this tool not only to display networking settings but also to perform certain operations. For example, the /renew option renews DHCP leases and the /flushdns option purges the DNS client resolver cache.

Retrieving Basic Settings with a Script

By writing scripts in VBScript that use WSH and WMI, system administrators can create more powerful and flexible tools to manage a broader range of Windows functionality.

For each setting in the Advanced TCP/IP Settings dialog box, WMI classes offer properties and methods that can retrieve and modify client network settings, the most important of which is the Win32_NetworkAdapterConfiguration class. Figure 3 illustrates which WMI classes correspond to different elements of the Advanced TCP/IP Settings dialog box.

Figure 3   How TCP/IP WMI Properties and Methods Correspond to the Windows UI

Figure 3   How TCP/IP WMI Properties and Methods Correspond to the Windows UI

For purposes of scripting, a WMI class is simply a way of packaging a related set of configuration settings and configuring them into a bundle with a recognizable name. Scripts call the properties to retrieve the settings and methods in order to change them.

Win32_NetworkAdapterConfiguration also includes properties and methods that correspond to the DNS, WMI, and Options tabs of the Advanced TCP/IP Settings dialog box. The sections that follow cover this in detail.

Using the Win32_NetworkAdapterConfiguration WMI Class

The 61 properties and 41 methods of Win32_NetworkAdapterConfiguration cover nearly all the settings and actions that are available through the Windows interface or command-line tools, and some that are not. To retrieve a property or call a method, only a few lines of VBScript code are required, as the following examples illustrate. For more information about the properties and methods for this WMI class, see the WMI Software Development Kit (SDK) topic at https://go.microsoft.com/fwlink/?LinkId=29991.

Calling the InstancesOf() or ExecQuery() methods of the SWbemServices object on Win32_NetworkAdapterConfiguration returns a collection of objects. This collection is in the form of an SWbemObjectSet collection containing zero or more SWbemObject instances. These objects and methods are documented in detail in the WMI Reference of the WMI SDK under "Scripting API for WMI."

Displaying One Setting

The following example displays the DHCPEnabled property of Win32_NetworkAdapterConfiguration. The DHCPEnabled property returns a Boolean value, which is a way of representing a condition that can be either true or false. The values used by VBScript are -1 for true and 0 for false; however, any non-zero value can be used programmatically to represent true. In most cases, scripts should use True and False, which are the two VBScript keywords, to represent these values.

The pattern used in this simple script can be repeated for any property of Win32_NetworkAdapterConfiguration except for those that return arrays. Handling properties that return arrays is discussed later in this paper.

Scripting Steps

  1. Connect to the WMI service using the "winmgmts:" moniker.

  2. Retrieve all the instances of the Win32_NetworkAdapterConfiguration class with the InstancesOf method.

    This returns a collection consisting of all the network adapter configurations on the computer.

  3. For each network adapter configuration in the collection, use the WSH Echo method to display the Boolean property corresponding to the DHCP Enabled setting in the IP addresses box on the IP Settings tab of the Advanced TCP/IP Settings dialog box.

Listing 1   Onesetting.vbs

  
1
2
3
4
5
6

Set objWMIService = GetObject("winmgmts:")
Set colNicConfig = _
 objWMIService.InstancesOf("Win32_NetworkAdapterConfiguration")
For Each objNicConfig In colNicConfig
  WScript.Echo objNicConfig.DHCPEnabled
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>onesetting.vbs

-1

0

0

0

0

0

0

0

note.gif  Note
A computer that contains only one physical network adapter may show settings for multiple network adapters. This is because some kinds of network connections, such as virtual private networks (VPNs), create their own virtual network adapters.

Displaying a Boolean Setting as a String

To produce more readable output, a minor change in line 5 causes the Boolean settings to be displayed as True or False rather than -1 or 0, which increases the readability of the script's output. By echoing "DHCP Enabled: " first and then concatenating the DHCPEnabled property, the script allows VBScript to "coerce" the Boolean setting into a string format.

Although all VBScript variables are type variant, VBScript automatically coerces (transforms) variants into appropriate data subtypes depending on the context in which they occur. In this case, because the script concatenates a variable of subtype Boolean onto a literal string, VBScript automatically converts the Boolean to its string equivalent.

Scripting Steps

  1. Connect to the WMI service.

  2. Retrieve all the instances of the Win32_NetworkAdapterConfiguration class with the InstancesOf method.

    This returns a collection consisting of all the network adapter configurations on the computer.

  3. For each network adapter configuration in the collection, use the WSH Echo method to display the caption "DHCP Enabled:" concatenated with the Boolean property DHCPEnabled.

Listing 2   Onesetting-string.vbs

  
1
2
3
4
5
6

Set objWMIService = GetObject("winmgmts:")
Set colNicConfig = _
 objWMIService.InstancesOf("Win32_NetworkAdapterConfiguration")
For Each objNicConfig In colNicConfig
  WScript.Echo "DHCP Enabled: " & objNicConfig.DHCPEnabled
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>onesetting-string.vbs

DHCP Enabled: True

DHCP Enabled: False

DHCP Enabled: False

DHCP Enabled: False

DHCP Enabled: False

DHCP Enabled: False

DHCP Enabled: False

DHCP Enabled: False

Displaying a Setting for Specific Network Adapters

The previous two examples display the value of the DHCPEnabled property for every network adapter configuration that WMI can find. Sometimes you may not want to work with all network adapters. For example, certain features, such as Routing and Remote Access and virtual private networks, may create their own virtual network adapter configurations on which TCP/IP is not enabled.

To filter for IP-enabled network adapters only, you can use the ExecQuery() method of the SWbemServices object, the object type returned by the GetObject() call on line 1. As a required parameter, ExecQuery() generally takes a string containing a query in WMI Query Language (WQL), a dialect of Structured Query Language (SQL). It returns an SWbemObjectSet collection of objects.

Although WQL queries are not case sensitive, putting all WQL keywords in caps is the standard convention because it makes the script more legible.

The most basic query used by system administrative scripts is "SELECT * FROM classname". This query returns all instances of the class, and the "*" serves as a wildcard character (as in SQL and command-line file system specifiers) that returns all the properties of each instance. For example, "SELECT * FROM Win32_Service" would return all the properties of all the instances of the Win32_Service class, which would represent all the services currently running on the computer.

In the following script, the WHERE keyword (also part of SQL) enables you to specify limiting conditions for the query. Here the query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True" requests all instances of the Win32_NetworkAdapterConfiguration class where the Boolean value of its IPEnabled property is True, in other words, for which IP is enabled.

This example also adds a second property, Index (see line 5), to distinguish between adapters when more than one is installed. Index is the key property for the Win32_NetworkAdapterConfiguration class, serving as the unique identifier for each instance of the class. The value of Index is an integer, starting with 0 for the first active network adapter configuration and incrementing by 1 for each successive one, that identifies a particular configuration.

Listing 3 returns the Index and DHCPEnabled properties for all adapters for which IPEnabled is True.

Scripting Steps

  1. Connect to the WMI service.

  2. Retrieve specific instances of the Win32_NetworkAdapterConfiguration class with the ExecQuery method and a query string that filters for those network adapters where the IPEnabled property is True.

    This returns a collection consisting of the network adapter configurations on the computer for which IP is enabled.

  3. For each network adapter configuration in the collection, use the WSH Echo method to display the index of the network adapter and the DHCPEnabled property.

Listing 3   Onesetting-execquery.vbs

  
1
2
3
4
5
6
7

Set objWMIService = GetObject("winmgmts:")
Set colNicConfig = objWMIService.ExecQuery("SELECT * FROM " & _
  "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfig
  WScript.Echo "Network Adapter: " & objNicConfig.Index
  WScript.Echo "  DHCP Enabled: " & objNicConfig.DHCPEnabled
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>onesetting-execquery.vbs

Network Adapter: 1

  DHCP Enabled: True

Network Adapter: 10

  DHCP Enabled: False

In other cases, you may want to restrict the script to running against just a single network adapter configuration. For example, on a dual-homed computer that is connected to two networks, you might want to enable DHCP on one network adapter but not the other. To do this, you could use a WQL query such as "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = 0."

A simpler way to accomplish the same thing is to use the Get() method of the SWbemServices object (returned by the GetObject call to the wingmgts: moniker). Get() takes a string containing a WMI object path as a required parameter and returns an SWbemObject object.

tip.gif  Tip
The following script displays a quirk of WMI: although VBScript, WSH, and WMI typically ignore white space, within the object path (the string parameter) passed to objWMIService.Get() there can be no space before or after the equal sign (=) in "Index=0." A space on either side of the equal sign causes an error.

Scripting Steps

  1. Connect to the WMI service.

  2. Retrieve a specific instance of the Win32_NetworkAdapterConfiguration class with the Get method and an object path that specifies an Index property (the key property) equal to 0.

    This returns a collection consisting of the network adapter configuration on the computer for which the index is 0.

  3. For this instance, use the WSH Echo method to display the index of the network adapter and the DHCPEnabled property.

Listing 4   Onesetting-onenic.vbs

  
1
2
3
4
5
6
7
8

intIndex = 1 ' index of an IP-enabled network adapter
Set objWMIService = GetObject("winmgmts:")
Set colNicConfig = objWMIService.ExecQuery("SELECT * FROM " & _
 "Win32_NetworkAdapterConfiguration WHERE Index = " & intIndex)
For Each objNicConfig In colNicConfig
  WScript.Echo "Network Adapter: " & objNicConfig.Index
  WScript.Echo "  DHCP Enabled: " & objNicConfig.DHCPEnabled
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>onesetting-onenic.vbs

Network Adapter: 1

  DHCP Enabled: True

Displaying Multi-Valued Properties

Because Windows allows a single network adapter setting to have more than one IP address, subnet mask, default gateway, or DNS server, some properties of Win32_NetworkAdapterConfiguration can have multiple values. WMI returns these multiple values in the form of an array. Because an array contains multiple values, it cannot be handled in the same way as a single string, Boolean, or number.

VBScript offers two techniques for transforming arrays into a string format that can be displayed with WScript.Echo: the Join function and the For Each loop.

Using the VBScript Join function

The VBScript Join function concatenates or joins together the elements of an array into a single string that is separated by a character or characters specified in an optional second parameter. If you do not specify this delimiter, Join uses a single space.

The Join function is necessary because you cannot directly display an array. First, you must divide the array into its elements or convert the array into a string. Join allows you to control the formatting of this string. For example, you can use either commas or colons (rather than spaces) to separate the elements of the array.

Scripting Steps

  1. Connect to the WMI service.

  2. Retrieve all the instances of the Win32_NetworkAdapterConfiguration class with the ExecQuery method and a query string that filters for those network adapters where the IPEnabled property is True.

    This returns a collection consisting of the network adapter configurations on the computer for which IP is enabled.

  3. For each network adapter configuration in the collection, use the WSH Echo method to display the index of the network adapter.

  4. If the IPAddress property (an array):

    • Is not Null, use the VBScript Join function to concatenate the elements of the IPAddress array into a string with elements separated by a space (the default) and assign them to a variable.

    • Is Null, assign an empty string to the variable.

  5. Display the space-delimited string of IP addresses.

Listing 5   Onesetting-array-join.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12

Set objWMIService = GetObject("winmgmts:")
Set colNicConfig = objWMIService.ExecQuery("SELECT * FROM " & _
 "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfig
  WScript.Echo "Network Adapter: " & objNicConfig.Index
  If Not IsNull(objNicConfig.IPAddress) Then
    strIPAddresses = Join(objNicConfig.IPAddress)
  Else
    strIPAddresses = ""
  End If
  WScript.Echo "  IP Address(es): " & strIPAddresses
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>onesetting-array-join.vbs

Network Adapter: 1

  IP Address(es): 0.0.0.0

Network Adapter: 10

  IP Address(es): 192.168.1.2

Using For Each ... In ... Next

The For Each loop provides an alternative way to display the elements of an array. This statement iterates through the array, allowing the script to separately perform an action on each element. In this case, we merely display the element.

Scripting Steps

  1. Connect to the WMI service.

  2. Retrieve all the instances of the Win32_NetworkAdapterConfiguration class with the ExecQuery method and a query string that filters for those network adapters where the IPEnabled property is True.

    This returns a collection consisting of the network adapter configurations on the computer for which IP is enabled.

  3. For each network adapter configuration in the collection, use the WSH Echo method to display the index of the network adapter.

  4. If the IPAddress property (an array) is not Null, use a For Each loop to iterate through the IPAddress array, displaying each element with the WSH Echo method.

Listing 6 Onesetting-array-foreach.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12

Set objWMIService = GetObject("winmgmts:")
Set colNicConfig = objWMIService.ExecQuery("SELECT * FROM " & _
 "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfig
  WScript.Echo "Network Adapter: " & objNicConfig.Index
  WScript.Echo "  IP Address(es):"
  If Not IsNull(objNicConfig.IPAddress) Then
    For Each strIPAddress In objNicConfig.IPAddress
      WScript.Echo "    " & strIPAddress
    Next
  End If
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>onesetting-array-foreach.vbs

Network Adapter: 1

  IP Address(es):

    0.0.0.0

Network Adapter: 10

  IP Address(es):

    192.168.1.2

Displaying a Range of Networking Properties

When inventorying network clients, you might often want to gather information about a larger selection of TCP/IP settings. For example, you might want to collect the settings displayed in the Advanced TCP/IP Settings dialog box and those shown by Ipconfig.exe. There is overlap between the two but also some differences. A script can combine all the settings from these sources into one package.

For each IP-enabled network adapter on a specific computer, use Ipsettings.vbs to obtain the information that displays on the IP Settings tab of the Advanced TCP/IP Settings dialog box for a network connection as well as the information that Ipconfig.exe (used with a few parameters) displays.

A network adapter configuration can have multiple IP addresses, subnets, default gateways, and gateway metrics.

Scripting Steps

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to “impersonate.”

  3. Use the ExecQuery method to query the Win32_NetworkAdapterConfiguration class, filtering the WQL query with “WHERE IPEnabled = True.”

    This returns a collection consisting of all the network adapter configurations on the computer for which IP is enabled.

  4. For each network adapter configuration in the collection, use the WSH Echo method to display the properties corresponding to the settings on the IP Settings tab.

  5. For those properties that return an array, use the VBScript IsNull() function to check whether the array is null.

    • If the array is not null, use the VBScript Join() function to concatenate the array elements into a string and display the string.

    • If the array is null, display an empty string.

Listing 7   Ipsettings.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

On Error Resume Next
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
WScript.Echo VbCrLf & "IP Settings"
 
For Each objNicConfig In colNicConfigs
 
  WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
  WScript.Echo "    " & objNicConfig.Description & VbCrLf
  WScript.Echo "    DHCP Enabled:                   " & _
   objNicConfig.DHCPEnabled
  If Not IsNull(objNicConfig.IPAddress) Then
    strIPAddresses = Join(objNicConfig.IPAddress)
  Else
    strIPAddresses = ""
  End If
  WScript.Echo "    IP Address(es):                 " & strIPAddresses
  If Not IsNull(objNicConfig.IPSubnet) Then
    strIPSubnet = Join(objNicConfig.IPSubnet)
  Else
    strIPSubnet = ""
  End If
  WScript.Echo "    Subnet Mask(s):                 " & strIPSubnet
  If Not IsNull(objNicConfig.DefaultIPGateway) Then
    strDefaultIPGateway = Join(objNicConfig.DefaultIPGateway)
  Else
    strDefaultIPGateway = ""
  End If
  WScript.Echo "    Default Gateways(s):            " & strDefaultIPGateway
  If Not IsNull(objNicConfig.GatewayCostMetric) Then
    strGatewayCostMetric = Join(objNicConfig.GatewayCostMetric)
  Else
    strGatewayCostMetric = ""
  End If
  WScript.Echo "    Gateway Metric(s):              " & strGatewayCostMetric
  WScript.Echo "    Interface Metric:               " & _
   objNicConfig.IPConnectionMetric
  WScript.Echo "    Connection-specific DNS Suffix: " & _
   objNicConfig.DNSDomain
 
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>ipsettings.vbs

 

IP Settings

 

  Network Adapter 1

    Intel(R) PRO/1000 MT Network Connection - Packet Scheduler Miniport

 

    DHCP Enabled:                   True

    IP Address(es):                 0.0.0.0

    Subnet Mask(s):                 

    Default Gateways(s):            

    Gateway Metric(s):              

    Interface Metric:               1

    Connection-specific DNS Suffix:

 

  Network Adapter 10

    3Com EtherLink 10/100 PCI For Complete PC Management NIC (3C905C-TX) - Packet Scheduler Miniport

 

    DHCP Enabled:                   False

    IP Address(es):                 192.168.1.2

    Subnet Mask(s):                 255.255.255.0

    Default Gateways(s):            

    Gateway Metric(s):              

    Interface Metric:               20

    Connection-specific DNS Suffix:

Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter

Win32_NetworkAdapterConfiguration is closely related to Win32_NetworkAdapter, another WMI class. There is a one-to-one correspondence between instances of the two classes and an implicit division of labor between the two classes: Win32_NetworkAdapter exposes mostly hardware-related properties, and in contrast to Win32_NetworkAdapterConfiguration, includes no methods. There is some overlap between the two classes: for example, both have a MACAddress property that retrieves the physical address of a network adapter.

Figure 4 illustrates the relationship between the Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter classes.

Only Win32_NetworkAdapter, however, has a NetConnectionID property (available only on Windows XP and Windows Server 2003) that returns the name of the Network Connection (from Network Connections) that is bound to the network adapter, even though such a software setting might more logically belong in Win32_NetworkAdapterConfiguration. This NetConnectionID property corresponds to the name that Ipconfig.exe uses for each network adapter.

In addition, Win32_NetworkAdapter alone includes an AdapterType property that describes the network medium to which the adapter connects, such as Ethernet 802.3 or Token Ring 802.5. Ipconfig.exe also uses this information to describe the network adapter.

Figure 4   Relationship Between These WMI Classes

Figure 4   Relationship Between These WMI Classes

The following examples show how to display the properties of Win32_NetworkAdapter and also the methods for correlating properties from instances of Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration.

Displaying Network Adapter Properties

Displaying network adapter settings requires similar scripting techniques to those involved in displaying TCP/IP settings. The only difference is that you use the Win32_NetworkAdapterConfiguration class to display TCP/IP settings and the Win32_NetworkAdapter class to display network adapter properties. For more information about the properties and methods for this WMI class, see the WMI Software Development Kit (SDK) topic at https://go.microsoft.com/fwlink/?LinkId=29992.

Scripting Steps

Listing 8 retrieves the properties for all the network adapters on a computer. This script retrieves some properties available only on Windows XP and Windows Server 2003.

  1. Create a variable to specify the computer name. For example, to specify the local computer, use (".").

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to “impersonate.”

  3. Use the ExecQuery method to query the Win32_NetworkAdapter class.

    This returns a collection consisting of all the network adapters on the computer.

  4. For each network adapter in the collection, use the WSH Echo method to display its properties.

  5. For those properties that return an array, use the VBScript IsNull() function to check whether the array is null.

    • If the array is not null, use the VBScript Join() function to concatenate the array elements into a string and display the string.

    • If the array is null, display an empty string.

Listing 8   Nicsettings.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

On Error Resume Next
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNics = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapter")
 
WScript.Echo VbCrLf & "Network Adapter Settings"
 
For Each objNic In colNics
 
  WScript.Echo VbCrLf & "  Network Adapter (Device ID)" & _
   objNic.DeviceID
  Wscript.Echo "    Index: " & objNic.Index
  Wscript.Echo "    MAC Address: " & objNic.MACAddress
  Wscript.Echo "    Adapter Type: " & objNic.AdapterType
  Wscript.Echo "    Adapter Type Id: " & objNic.AdapterTypeID
  Wscript.Echo "    Description: " & objNic.Description
  Wscript.Echo "    Manufacturer: " & objNic.Manufacturer
  Wscript.Echo "    Name: " & objNic.Name
  Wscript.Echo "    Product Name: " & objNic.ProductName
  Wscript.Echo "    Net Connection ID: " & objNic.NetConnectionID
  Wscript.Echo "    Net Connection Status: " & objNic.NetConnectionStatus
  Wscript.Echo "    PNP Device ID: " & objNic.PNPDeviceID
  Wscript.Echo "    Service Name: " & objNic.ServiceName
  If Not IsNull(objNic.NetworkAddresses) Then
    strNetworkAddresses = Join(objNic.NetworkAddresses)
  Else
    strNetworkAddresses = ""
  End If
  Wscript.Echo "    NetworkAddresses: " & strNetworkAddresses
  Wscript.Echo "    Permanent Address: " & objNic.PermanentAddress
  Wscript.Echo "    AutoSense: " & objNic.AutoSense
  Wscript.Echo "    Maximum Number Controlled: " & objNic.MaxNumberControlled
  Wscript.Echo "    Speed: " & objNic.Speed
  Wscript.Echo "    Maximum Speed: " & objNic.MaxSpeed
 
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>nicsettings.vbs

 

Network Adapter Settings

 

  Network Adapter (Device ID)1

    Index: 1

    MAC Address: 00:0D:56:15:ED:B7

    Adapter Type: Ethernet 802.3

    Adapter Type Id: 0

    Description: Intel(R) PRO/1000 MT Network Connection

    Manufacturer: Intel

    Name: Intel(R) PRO/1000 MT Network Connection

    Product Name: Intel(R) PRO/1000 MT Network Connection

    Net Connection ID: Local Area Connection

    Net Connection Status: 7

    PNP Device ID: PCI\VEN_8086&DEV_100E&SUBSYS_01511028&REV_02\4&1C660DD6&0&60F0

    Service Name: E1000

    NetworkAddresses:

    Permanent Address:

    AutoSense:

    Maximum Number Controlled: 0

    Speed:

    Maximum Speed:

 

  Network Adapter (Device ID)2

    Index: 2

    MAC Address:

    Adapter Type:

    Adapter Type Id:

    Description: RAS Async Adapter

    Manufacturer:

    Name: RAS Async Adapter

    Product Name: RAS Async Adapter

    Net Connection ID:

    Net Connection Status:

    PNP Device ID:

    Service Name:

    NetworkAddresses:

    Permanent Address:

    AutoSense:

    Maximum Number Controlled: 0

    Speed:

    Maximum Speed:

 

  Network Adapter (Device ID)3

    Index: 3

    MAC Address:

    Adapter Type:

    Adapter Type Id:

    Description: WAN Miniport (L2TP)

    Manufacturer: Microsoft

    Name: WAN Miniport (L2TP)

    Product Name: WAN Miniport (L2TP)

    Net Connection ID:

    Net Connection Status:

    PNP Device ID: ROOT\MS_L2TPMINIPORT\0000

    Service Name: Rasl2tp

    NetworkAddresses:

    Permanent Address:

    AutoSense:

    Maximum Number Controlled: 0

    Speed:

    Maximum Speed:

 

  Network Adapter (Device ID)6

    Index: 6

    MAC Address:

    Adapter Type:

    Adapter Type Id:

    Description: Direct Parallel

    Manufacturer: Microsoft

    Name: Direct Parallel

    Product Name: Direct Parallel

    Net Connection ID:

    Net Connection Status:

    PNP Device ID: ROOT\MS_PTIMINIPORT\0000

    Service Name: Raspti

    NetworkAddresses:

    Permanent Address:

    AutoSense:

    Maximum Number Controlled: 0

    Speed:

    Maximum Speed:

 

  Network Adapter (Device ID)8

    Index: 8

    MAC Address: 00:0D:56:15:ED:B7

    Adapter Type: Ethernet 802.3

    Adapter Type Id: 0

    Description: Packet Scheduler Miniport

    Manufacturer: Microsoft

    Name: Packet Scheduler Miniport

    Product Name: Packet Scheduler Miniport

    Net Connection ID:

    Net Connection Status:

    PNP Device ID: ROOT\MS_PSCHEDMP\0000

    Service Name: PSched

    NetworkAddresses:

    Permanent Address:

    AutoSense:

    Maximum Number Controlled: 0

    Speed:

    Maximum Speed:

 

  Network Adapter (Device ID)10

    Index: 10

    MAC Address: 00:0A:5E:3D:E3:70

    Adapter Type: Ethernet 802.3

    Adapter Type Id: 0

    Description: 3Com EtherLink 10/100 PCI For Complete PC Management NIC (3C905C-TX)

    Manufacturer: 3Com

    Name: 3Com EtherLink 10/100 PCI For Complete PC Management NIC (3C905C-TX)

    Product Name: 3Com EtherLink 10/100 PCI For Complete PC Management NIC (3C905C-TX)

    Net Connection ID: Local Area Connection 2

    Net Connection Status: 2

    PNP Device ID: PCI\VEN_10B7&DEV_9200&SUBSYS_100010B7&REV_78\4&1C660DD6&0&40F0

    Service Name: EL90Xbc

    NetworkAddresses:

    Permanent Address:

    AutoSense:

    Maximum Number Controlled: 0

    Speed:

    Maximum Speed:

Associating the Network Connections Name with MAC and IP Addresses Using Two Classes

For various purposes, administrators may need to retrieve the MAC or physical addresses and IP addresses of computers and correlate them with the name of the adapter listed in Network Connections. Ipconfig.exe uses the network adapter name and type to distinguish between network adapters, as shown in Figure 5.

Associating MAC and IP addresses corresponds to the part of the IP routing process performed by the Address Resolution Protocol (ARP). You can view the resulting IP - MAC address translation tables by using the command-line tool Arp.exe. Figure 5 provides an example of the output obtained by running the arp -a command.

Figure 5   Arp.exe output

Figure 5   Arp.exe output

To use WMI to connect MAC and IP addresses with network adapter names and types, you must correlate properties from corresponding instances of Win32_NetworkAdapterConfiguration (MACAddress and IPAddress) and Win32_NetworkAdapter (NetConnectionID and AdapterType). The NetConnectionID property of Win32_NetworkAdapter is available only on Windows XP and Windows Server 2003.

In order to find the specific instances of each class that correspond to each other, you can use the Key qualifier to determine key properties for each class. Key properties (there can be more than one) together supply a unique reference for each class instance and are part of the instance namespace handle. They are a little like the key field in a database. The WMI SDK or Wbemtest.exe (the WMI Tester tool, which is included on all versions of Windows that include WMI) can tell you which property or properties of a class are keys.

Table 2 displays the key properties for the two WMI network adapter classes.

Table 2   Key Properties for WMI Network Adapter Classes

Class

Key Property

Win32_NetworkAdapterConfiguration

Index

Win32_NetworkAdapter

DeviceID

By using either of these keys, you can match instances of the two classes. In the following example, the Get property of SWbemServices is used to retrieve the instance of Win32_NetworkAdapter, whose DeviceID property corresponds to the Index property of a specific Win32_NetworkAdapterConfiguration instance.

Getncmacip.vbs displays the network connection name (or index number for pre-Windows XP clients), MAC address, IP addresses, and subnet masks for IP-enabled network interfaces. A network adapter configuration can have more than one IP address and subnet. The script gets properties from corresponding instances of Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration by using the direct correlation between Win32_NetworkAdapter.DeviceID and Win32_NetworkAdapterConfiguration.Index.

The script shows a simpler way than the GetNetConnectionID() function in Ipsettings.vbs to determine whether the NetConnectionID property is available. Instead of checking the operating system version to see if it is Windows XP (which would ensure that the Win32_NetworkAdapter.NetConnectionID was available), it simply tries to retrieve the NetConnectionID property. If an error is returned, it uses the Win32_NetworkAdapterConfiguration.Index property instead.

Scripting Steps

Listing 9 retrieves the IP addresses and subnet masks for each network adapter on a single computer.

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to “impersonate.”

  3. Use the ExecQuery method to query the Win32_NetworkAdapterConfiguration class, filtering the WQL query with “WHERE IPEnabled = True.”

    This returns a collection consisting of all the network adapter configurations on the computer for which IP is enabled.

  4. Iterate through each network adapter configuration in the collection, performing the following steps.

  5. Use the Get method of the objWMIService object to fetch the instance of Win32_NetworkAdapter whose DeviceID property equals the Index property of Win32_NetworkAdapterConfiguration.

  6. Display the AdapterType and NetConnectionID properties of the current instance of Win32_NetworkAdapter.

    If an error occurs, the NetConnectionID property does not exist (this property is new to Windows XP and Windows Server 2003); so switch back to displaying the Index property of Win32_NetworkAdapterConfiguration to identify the current network adapter.

  7. Display the Description property of Win32_NetworkAdapterConfiguration and the MACAddress property of Win32_NetworkAdapter.

  8. Display the IPAddress and IPSubnet properties of Win32_NetworkAdapterConfiguration.

Listing 9   Getncmacip.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

On Error Resume Next
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
WScript.Echo VbCrLf & "MAC & IP Addresses & Subnet Masks"
 
For Each objNicConfig In colNicConfigs
 
  Set objNic = objWMIService.Get _
   ("Win32_NetworkAdapter.DeviceID=" & objNicConfig.Index)
 
  WScript.Echo VbCrLf & "  " & objNic.AdapterType & " " & _
   objNic.NetConnectionID
  If Err Then
    WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
  End If
 
  WScript.Echo "    " & objNicConfig.Description & VbCrLf 
  WScript.Echo "    MAC Address:" & VbCrLf & _
   "        " & objNic.MACAddress
  WScript.Echo "    IP Address(es):"
  For Each strIPAddress In objNicConfig.IPAddress
    WScript.Echo "        " & strIPAddress
  Next
  WScript.Echo "    Subnet Mask(s):"
  For Each strIPSubnet In objNicConfig.IPSubnet
    WScript.Echo "        " & strIPSubnet
  Next
 
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>getncmacip.vbs

 

MAC & IP Addresses & Subnet Masks

 

  Ethernet 802.3 Local Area Connection

    Intel(R) PRO/1000 MT Network Connection - Packet Scheduler Miniport

 

    MAC Address:

        00:0D:56:15:ED:B7

    IP Address(es):

        0.0.0.0

    Subnet Mask(s):

 

  Ethernet 802.3 Local Area Connection 2

    3Com EtherLink 10/100 PCI For Complete PC Management NIC (3C905C-TX) - Packet Scheduler Miniport

 

    MAC Address:

        00:0A:5E:3D:E3:70

    IP Address(es):

        192.168.1.2

    Subnet Mask(s):

        255.255.255.0

Using Association Classes to Retrieve Correlated Data

Another way to get data from related instances of Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter is to use the association class, Win32_NetworkAdapterSetting. In this case, the properties to be correlated are the NetConnectionID and MACAddress properties of Win32_NetworkAdapter and the IPAddress and IPSubnet properties of Win32_NetworkAdapterConfiguration.

Figure 6 illustrates how the Win32_NetworkAdapterSetting association class is used to obtain data from related instances of these two classes. This example uses an ASSOCIATORS OF query with the ExecQuery method of SWbemServices to associate properties from the two classes. ASSOCIATORS OF is a keyword of WMI Query Language (WQL).

Figure 6   Correlating Data by Using Win32_NetworkAdapterSetting

Figure 6   Correlating Data by Using Win32_NetworkAdapterSetting

Associating the Network Connections Name with IP Addresses Using an Association Class

Getncmacip-assoc.vbs displays the network connection name (called an index number on clients that run an operating system older than Windows XP), MAC address, IP addresses, and subnet masks for IP-enabled network interfaces. It uses the Win32_NetworkAdapterSetting association class to correlate properties from Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter. A network adapter configuration can have more than one IP address and subnet.

Scripting Steps

Listing 10 retrieves the TCP/IP network client properties on a single computer.

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to “impersonate.”

  3. Use the ExecQuery method to query the Win32_NetworkAdapterConfiguration class, filtering the WQL query with “WHERE IPEnabled = True.”

    This returns a collection consisting of all the network adapter configurations on the computer for which IP is enabled.

  4. For each network adapter configuration in the collection, use the ExecQuery method to query the Win32_NetworkAdapterConfiguration class with an ASSOCIATORS OF query. The query returns a collection of instances of Win32_NetworkAdapter.

    An ASSOCIATORS OF query uses the Win32_NetworkAdapterSetting association class to correlate the instances of Win32_NetworkAdapterConfiguration (identified by the Index property) with the corresponding instances of the associated class, Win32_NetworkAdapter.

  5. Iterate through the collection of instances of Win32_NetworkAdapter, displaying the AdapterType, NetConnectionID, and MACAddress properties.

    If an error occurs when displaying the NetConnectionID property, display instead the Index of the corresponding instance of Win32_NetworkAdapterConfiguration.

  6. Display the IPAddress and IPSubnet properties for the instance of Win32_NetworkAdapterConfiguration associated with the current instance of Win32_NetworkAdapter.

Listing 10   Getncmacip-assoc.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

On Error Resume Next
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
WScript.Echo VbCrLf & "MAC & IP Addresses & Subnet Masks"
 
For Each objNicConfig In colNicConfigs
 
  Set colNics = objWMIService.ExecQuery _
   ("ASSOCIATORS OF " & "{Win32_NetworkAdapterConfiguration.Index='" & _
   objNicConfig.Index & "'}" & _
   " WHERE AssocClass=Win32_NetworkAdapterSetting")
 
  For Each objNic in colNics
 
    WScript.Echo VbCrLf & "  " & objNic.AdapterType & " " & _
     objNic.NetConnectionID
    If Err Then
      WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
    End If
 
    WScript.Echo "    " & objNicConfig.Description & VbCrLf 
    WScript.Echo "    MAC Address:" & VbCrLf & _
     "        " & objNic.MACAddress
    WScript.Echo "    IP Address(es):"
    For Each strIPAddress In objNicConfig.IPAddress
      WScript.Echo "        " & strIPAddress
    Next
    WScript.Echo "    Subnet Mask(s):"
    For Each strIPSubnet In objNicConfig.IPSubnet
      WScript.Echo "        " & strIPSubnet
    Next
 
  Next
 
Next

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>getncmacip-assoc.vbs

 

MAC & IP Addresses & Subnet Masks

 

  Ethernet 802.3 Local Area Connection

    Intel(R) PRO/1000 MT Network Connection - Packet Scheduler Miniport

 

    MAC Address:

        00:0D:56:15:ED:B7

    IP Address(es):

        0.0.0.0

    Subnet Mask(s):

 

  Ethernet 802.3 Local Area Connection 2

    3Com EtherLink 10/100 PCI For Complete PC Management NIC (3C905C-TX) - Packet Scheduler Miniport

 

    MAC Address:

        00:0A:5E:3D:E3:70

    IP Address(es):

        192.168.1.2

    Subnet Mask(s):

        255.255.255.0

Displaying Settings Filtered for Network Connections Name

Now that you know how to use a single script to retrieve related properties from Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter, here is another way you might want to use this capability. If you need information from only one network adapter, you could connect to it by using its name under Network Connections — a more mnemonic identifier than the index number — and retrieve a setting from that network adapter. You can do this by using the NetConnectionID property of Win32_NetworkAdapter.

The NetConnectionID property is available only on Windows XP and Windows Server 2003. If the property is not found, the script generates a run-time error, which is handled by checking the VBScript Err object. If the Err object's default property, Number, is not 0, an error has occurred; in response, the script displays an error message and ends.

Scripting Steps

Listing 11 retrieves the TCP/IP network client properties on a single computer.

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to “impersonate.”

  3. Use the ExecQuery method to query the Win32_NetworkAdapter class, filtering the WQL query with a WHERE clause for a specific NetConnectionID.

    This returns a collection of one instance representing the network adapter with that particular NetConnectionID.

  4. Check that the NetConnectionID was found. If it was, proceed with the tasks of the script.

  5. For each network adapter in the collection, correlate each network adaptor configuration with a network adapter by using an ASSOCIATORS OF query. To do this, you must use the DeviceID property of Win32_NetworkAdapter, which is the key property for this class (lines 12–15).

  6. For each network adapter and associated configuration, display the adapter type and IP addresses.

  7. If the specific NetConnectionID was not found, display a message.

Listing 11   Getncip.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

On Error Resume Next
 
strComputer = "."
strNetConn = "Local Area Connection"
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNics = objWMIService.ExecQuery("SELECT * FROM " & _
 "Win32_NetworkAdapter WHERE NetConnectionID = '" & strNetConn & "'")
 
If colNics.Count > 0 Then
  For Each objNic in colNics
    Set colNicConfigs = objWMIService.ExecQuery ("ASSOCIATORS OF " & _
     "{Win32_NetworkAdapter.DeviceID='" & objNic.DeviceID & "'}" & _
     " WHERE AssocClass=Win32_NetworkAdapterSetting")
    For Each objNicConfig In colNicConfigs
      WScript.Echo VbCrLf & "  " & objNic.AdapterType & " " & _
       objNic.NetConnectionID
      If Not IsNull(objNicConfig.IPAddress) Then
        For Each strIPAddress in objNicConfig.IPAddress
          Wscript.Echo "    IP Address: " &  strIPAddress
        Next
      End If
    Next
  Next
Else
  WScript.Echo "Unable to find NetconnectID named '" & strNetConn & "'." & _
   VbCrLf & "If Windows 2000 or earlier, NetConnectionID property unavailable."
End If

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>getncip.vbs

 

  Ethernet 802.3 Local Area Connection

    IP Address: 192.168.0.213

Displaying Expanded IP Configuration Data

In Windows XP, a convenient way to retrieve more detailed TCP/IP client settings is through the Network Connections Details dialog box for a specific network connection.

To retrieve detailed TCP/IP settings for a specific network connection

  1. Open Control Panel and then double-click Network Connections.

  2. In the Network Connections dialog box, right-click a specific network connection and then click Status.

  3. On the Support tab, click Details to view network connection settings.

Figure 7 shows an example of a Network Connection Details box.

Figure 7   Network Connection Details Dialog Box

Figure 7   Network Connection Details Dialog Box

Using Ipconfig.exe to Display Expanded Configuration Data

You can use the /all option with the Ipconfig.exe command-line tool to retrieve a collection of settings even more detailed than those in the Network Connections Details dialog box. Figure 8 shows screen output like what displays when you use the ipconfig /all command.

Figure 8   Using Ipconfig.exe /all to Display Network Data

Figure 8   Using Ipconfig.exe /all to Display Network Data

Using a Script to Display Expanded Configuration Data

WMI provides the functionality to reproduce nearly all the functionality of the ipconfig /all command, along with many other settings. Besides Win32_NetworkAdapterConfiguration, Ipconfig.vbs also uses StdRegProv, which is a class included in the System Registry WMI provider, to extract some settings from the registry.

caution.gif  Caution
Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

Scripting Steps

Listing 12 retrieves an extensive set of TCP/IP network client properties, equivalent to those retrieved by Ipconfig.exe /all, from a single computer. The script uses two functions: one that converts the dates returned to a more readable format and another that returns the operating system version.

To carry out these tasks, the script must:

  1. Invoke On Error Resume Next so that errors do not terminate the script.

  2. Assign values to constants and variables to be used.

  3. Use a GetObject call to connect to the StdRegProv class in the WMI namespace root\default.

  4. Retrieve global settings from the registry by using the GetStringValue and GetDWORDValue methods of StdRegProv. These settings apply to all network adapters.

    These methods return a value in an out parameter, the final parameter for each method.

  5. Use a Select Case statement to decode the value returned by the dwNodeType out parameter.

  6. Use an If ... ElseIf ... Else statement to decode the value returned by the dwIPRouting out parameter.

  7. Use a GetObject call to connect to the WMI namespace root\cimv2.

  8. Use the ExecQuery method to query the Win32_NetworkAdapterConfiguration class, filtering the WQL query with “WHERE IPEnabled = True.”

    This returns a collection consisting of all the network adapter configurations on the computer for which IP is enabled.

  9. Retrieve the value of DNSEnabledForWINSResolution, which is a global setting, the same for each network adapter. Use an If ... ElseIf ... Else statement to decode the Boolean value returned by DNSEnabledForWINSResolution.

  10. Display the global settings retrieved.

  11. Again use the ExecQuery method to query the Win32_NetworkAdapterConfiguration class, filtering the WQL query with “WHERE IPEnabled = True.”

  12. Call the GetOSVer function, which gets the Version property of Win32_OperatingSystem. Assign the value returned to a variable.

  13. Iterate through each network adapter configuration in the collection returned by the query.

  14. Retrieve the Index property for the instance and use the Get method to return the instance of Win32_NetworkAdapter whose DeviceID property corresponds to the value of Index.

  15. Retrieve the per-adapter properties of Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration displayed by Ipconfig.exe /all.

  16. If the AdapterType property is not available, display "Network" as the adapter type.

  17. If the operating system version is:

    • Greater than 5, indicating Windows XP or Windows Server 2003, retrieve the NetConnectionID property from Win32_NetworkAdapter.

    • 5 or less, use the Index property of Win32_NetworkAdapterConfiguration as the identifier of the network adapter.

  18. Retrieve and display other properties of the two classes.

  19. If the property returns an array, check that the value is not null. If the return value is not null, assign each array element, separated by a space, to a string, and display the string.

  20. If the property returns a value in WMI DATETIME format, call the WMIDateStringToDate function and pass it the DATETIME value.

    This function converts the value to a more readable string representing the date and time.

Listing 12   Ipconfig.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

On Error Resume Next
 
 
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath1 = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
strKeyPath2 = "SYSTEM\CurrentControlSet\Services\NetBT\Parameters"
strHostEntry = "Hostname"
strDomainEntry = "Domain"
strNodeEntry = "DhcpNodeType"
strRoutingEntry = "IPEnableRouter"
 
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strHostEntry,strHostname
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strDomainEntry,strDomain
objReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath2,strNodeEntry,dwNodeType
objReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath1,strRoutingEntry,dwIPRouting
 
Select Case dwNodeType
  Case 4 strNodeType = "Mixed"
  Case 8 strNodeType = "Hybrid"
  Case Else strNodeType = dwNodeType
End Select
If dwIPRouting = 0 Then
  strIPRouting = "No"
ElseIf dwIPRouting = 1 Then
  strIPRouting = "Yes"
Else
  strIPRouting = "?"
End If
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colFirstNicConfig = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objFirstNicConfig In colFirstNicConfig
  strDnsWins = objFirstNicConfig.DNSEnabledForWINSResolution
Next
If strDnsWins = False Then
  strWinsProxy = "No"
ElseIf strDnsWins = True Then
  strWinsProxy = "Yes"
Else
  strWinsProxy = "?"
End If
 
' Display global settings.
 
WScript.Echo VbCrLf & "Windows IP Configuration" & VbCrLf
WScript.Echo "        Host Name . . . . . . . . . . . . : " & strHostname
WScript.Echo "        Primary DNS Suffix  . . . . . . . : " & strDomain
WScript.Echo "        Node Type . . . . . . . . . . . . : " & strNodeType
WScript.Echo "        IP Routing Enabled. . . . . . . . : " & strIPRouting
WScript.Echo "        WINS Proxy Enabled. . . . . . . . : " & strWinsProxy
WScript.Echo "        DNS Suffix Search List. . . . . . : " & strDomain
 
Set colNicConfigs = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
sngOsVer = GetOsVer
 
' Display per-adapter settings.
 
For Each objNicConfig In colNicConfigs
  intIndex = objNicConfig.Index
  Set objNic = objWMIService.Get("Win32_NetworkAdapter.DeviceID=" & intIndex)
 
  strAdapterType = objNic.AdapterType
  If IsEmpty(strAdapterType) Or IsNull(strAdapterType) Or _
   (strAdapterType = "") Then
    strAdapterType = "Network"
  End If
 
  If sngOsVer > 5 Then
    strNetConn = objNic.NetConnectionID
  Else
    strNetConn = intIndex
  End If
 
  WScript.Echo VbCrLf & strAdapterType & " adapter " & strNetConn
  WScript.Echo "        Connection-specific DNS Suffix  . : " & _
   objNicConfig.DNSDomain
  WScript.Echo "        Description . . . . . . . . . . . : " & _
   objNicConfig.Description
  WScript.Echo "        Physical Address. . . . . . . . . : " & _
   objNicConfig.MACAddress
  WScript.Echo "        DHCP Enabled. . . . . . . . . . . : " & _
   objNicConfig.DHCPEnabled
'  WScript.Echo "        Autoconfiguration Enabled . . . .: " & objNicConfig.?
 
  strIPAddresses = ""
  If Not IsNull(objNicConfig.IPAddress) Then
    For Each strIPAddress In objNicConfig.IPAddress
      strIPAddresses = strIPAddresses & strIPAddress & " "
    Next
  End If
  WScript.Echo "        IP Address. . . . . . . . . . . . : " & strIPAddresses
  strIPSubnets = ""
  If Not IsNull(objNicConfig.IPSubnet) Then
    For Each strIPSubnet In objNicConfig.IPSubnet
      strIPSubnets = strIPSubnets & strIPSubnet & " "
    Next
  End If
  WScript.Echo "        Subnet Mask . . . . . . . . . . . : " & strIPSubnets
  strDefaultIPGateways = ""
  If Not IsNull(objNicConfig.DefaultIPGateway) Then
    For Each strDefaultIPGateway In objNicConfig.DefaultIPGateway
      strDefaultIPGateways = strDefaultIPGateways & strDefaultIPGateway & " "
    Next
  End If
  WScript.Echo "        Default Gateway . . . . . . . . . : " & _
   strDefaultIPGateways
  WScript.Echo "        DHCP Server . . . . . . . . . . . : " & _
   objNicConfig.DHCPServer
  strDNSServerSearchOrder = ""
  If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
    For Each strDNSServer In objNicConfig.DNSServerSearchOrder
      strDNSServerSearchOrder = strDNSServerSearchOrder & VbCrLf & _
      "                                            " & strDNSServer
    Next
  End If
  WScript.Echo "        DNS Servers . . . . . . . . . . . :" & _
   strDNSServerSearchOrder
  If Not IsNull(objNicConfig.WINSPrimaryServer) Then
    WScript.Echo "        Primary WINS Server . . . . . . . : " & _
     objNicConfig.WINSPrimaryServer
  End If
  If Not IsNull(objNicConfig.WINSSecondaryServer) Then
    WScript.Echo "        Secondary WINS Server . . . . . . : " & _
     objNicConfig.WINSSecondaryServer
  End If
  If objNicConfig.DHCPEnabled Then
    dtmRawLeaseObtainedDate = objNicConfig.DHCPLeaseObtained
    strFormattedLeaseObtainedDate = WMIDateToString(dtmRawLeaseObtainedDate)
    WScript.Echo "        Lease Obtained. . . . . . . . . . : " & _
     strFormattedLeaseObtainedDate
    dtmRawLeaseExpiresDate = objNicConfig.DHCPLeaseExpires
    strFormattedLeaseExpiresDate = WMIDateToString(dtmRawLeaseExpiresDate)
    WScript.Echo "        Lease Expires . . . . . . . . . . : " & _
    strFormattedLeaseExpiresDate
  End If
Next
 
'******************************************************************************
' Function: WMIDateStringToDate(dtmDate)
' Converts WMI date to string.
'******************************************************************************
 
Function WMIDateToString(dtmDate)
    WMIDateToString = CDate(Mid(dtmDate, 5, 2) & "/" & _
                      Mid(dtmDate, 7, 2) & "/" & _
                      Left(dtmDate, 4) & " " & _
                      Mid(dtmDate, 9, 2) & ":" & _
                      Mid(dtmDate, 11, 2) & ":" & _
                      Mid(dtmDate, 13, 2))
End Function
 
'******************************************************************************
' Function: GetOsVer
' Gets the operating system version number.
'******************************************************************************
 
Function GetOsVer
  Set colOperatingSystems = objWMIService.ExecQuery _
   ("Select * from Win32_OperatingSystem")
  For Each objOperatingSystem In colOperatingSystems
    GetOSVer = CSng(Left(objOperatingSystem.Version, 3))
  Next
End Function

When you use Cscript.exe to run this script, output similar to the following is displayed in the command window:

C:\scripts>ipconfig.vbs

 

Windows IP Configuration

 

        Host Name . . . . . . . . . . . . . . : client1

        Primary DNS Suffix  . . . . . . . : fabrikam.com

        Node Type . . . . . . . . . . . . . . : Hybrid

        IP Routing Enabled. . . . . . . . : No

        WINS Proxy Enabled. . . . . . . : Yes

        DNS Suffix Search List. . . . . . : fabrikam.com

 

Ethernet 802.3 adapter Local Area Connection

        Connection-specific DNS Suffix  . :

        Description . . . . . . . . . . . : 3Com 3C920 Integrated Fast Ethernet

Controller (3C905C-TX Compatible) - Packet Scheduler Miniport

        Physical Address. . . . . . . . . : 00:B0:D0:23:70:37

        DHCP Enabled. . . . . . . . . . . : True

        IP Address. . . . . . . . . . . . : 192.168.0.12

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        Default Gateway . . . . . . . . . : 192.168.0.1

        DHCP Server . . . . . . . . . . . : 192.168.0.1

        DNS Servers . . . . . . . . . . . :

                                                   192.168.0.1

        Primary WINS Server . . . . . . . : 192.168.0.1

        Secondary WINS Server . . . . . . : 192.168.0.2

        Lease Obtained. . . . . . . . . . . . . : 6/2/2004 4:31:19 PM

        Lease Expires . . . . . . . . . . . . . : 6/12/2004 7:31:19 PM

Tools for Basic TCP/IP Networking on Clients

The tables in this section list the available command-line and scripting tools, and registry keys for retrieving and displaying basic networking data on clients. Table 3 lists the command-line tools for displaying basic TCP/IP networking on clients and also indicates where you find the tools.

Table 3   Command-Line Tools for Basic TCP/IP Networking on Clients

Tool

Where Available

Arp.exe

Windows operating systems1

Getmac.exe: GetMAC

Windows 2000 Resource Kit

Hostname.exe

Windows operating systems

Ipconfig.exe

Windows operating systems

Listadapters.vbs

Windows 2000 Resource Kit

Netconnections.vbs

Windows 2000 Resource Kit

Netipconfig.pl

Windows 2000 Resource Kit

Netipfilteringconfig.pl

Windows 2000 Resource Kit

Net.exe

Windows operating systems

Netset.exe

Windows 2000 Resource Kit

Netsh.exe

Windows operating systems

Netstat.exe

Windows operating systems

Networkprotocol.vbs

Windows 2000 Resource Kit

Ping.exe

Windows 2000 Resource Kit

Protocolbinding.vbs

Windows 2000 Resource Kit

Subnet_op.vbs

Windows 2000 Resource Kit

Timezone.exe: Daylight Saving Time Update Utility

Windows 2000 Resource Kit

Tzedit.exe: Time Zone Editor

Windows 2000 Resource Kit -- GUI tool

Wsremote.exe

Windows XP Support Tool

1 Windows 2000, Windows XP, and Windows Server 2003. May also be present on other versions of Windows.

Table 4 lists the WSH objects and WMI classes for displaying basic TCP/IP networking on clients and also provides notes and availability information.

Table 4   WSH Objects and WMI Classes for Basic TCP/IP Networking on Clients

Technology

Object or Class

Notes/Where Available

WSH

WshController

WshNetwork

WshShell

 

WMI

Win32_ComputerSystem

 

WMI

Win32_NetworkAdapterConfiguration

 

WMI

Win32_NetworkAdapter

 

WMI

Win32_NetworkAdapterSetting

Association class associating Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration

WMI

Win32_NetworkClient

 

WMI

Win32_NetworkConnection

 

WMI

Win32_NetworkProtocol

 

WMI

Win32_NTDomain

Windows XP and Windows Server 2003 only

WMI

Win32_OperatingSystem

 

WMI

Win32_PingStatus

Windows XP and Windows Server 2003 only

WMI

Win32_ProtocolBinding

Association class associating Win32_NetworkProtocol, Win32_SystemDriver, and Win32_NetworkAdapter

Table 5 lists the registry keys to use for displaying basic TCP/IP networking on clients.

caution.gif  Caution
Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

Table 5   Registry Subkeys Related to Basic TCP/IP Networking on Clients

Registry Subkeys

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
\{AdapterIdentifier}

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dhcp

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dnscache

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBIOS

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netman