Downloads
47100.zip

I'd like to be able to determine a computer's network ID based on its IP address and network mask. Can you provide a sample script or a template?

Determining networking details about a Windows computer was much more difficult before Windows Management Instrumentation (WMI) became a standard part of the OS. However, although WMI is useful, it's such an extensive technology that finding the classes, properties, and methods that will solve a problem is sometimes difficult. The first step in answering the above question is to understand WMI's view of network adapters. WMI has a fairly broad definition of network adapters that includes more than just physical adapters (e.g., to WMI, VPN connections are also adapters).

The IPAddress and IPSubnet properties of the Win32_NetworkAdapterConfiguration class provide the information necessary to answer the question. However, you need to consider several factors when using these properties.

First, not all adapters are IP-enabled. Second, it's possible (and sometimes typical) for a machine to have more than one physical adapter. For example, an edge firewall Internet Security and Acceleration (ISA) Server has at least two physical adapters (and sometimes more, depending on organizational requirements). Also, a physical adapter can have multiple IP addresses.

The IPEnabled property of the Win32_NetworkAdapterConfiguration class is a Boolean (true/false) value that indicates whether an adapter is IP-enabled. Also, because an adapter can have multiple IP addresses and network masks, WMI stores the IPAddress and IPSubnet properties as arrays. (I use the term network mask rather than subnet mask because the term subnet really refers to a subnet-divided network. Microsoft seems to use the term subnet mask when it really means network mask.) Based on this information, I've formulated a simple method for obtaining a computer's network ID.

The general approach for obtaining a computer's network ID by using WMI is roughly as follows:

  1. Obtain a collection of IP-enabled Win32_NetworkAdapterConfiguration objects.
  2. Retrieve the first adapter whose IPAddress and IPSubnet properties aren't null (i.e., retrieve the first adapter whose properties contain data).
  3. Retrieve the arrays containing the adapter's IP address(es) and network mask(s).
  4. Perform a bitwise AND operation on each octet of the first IP address in the array and its associated mask, and return the results. (This step assumes that the first IP address and network mask bound to the first Win32_NetworkAdapterConfiguration object in the collection is the desired address. More about this assumption in a moment.)

NetID.vbs (Listing 1) and NetID.js (Listing 2) follow this approach and are written in VBScript and JScript, respectively. For these scripts to obtain a computer's IP address and network mask, they must first compute the system's network ID.

Determining a computer's network ID. In WMI, IP addresses and network masks are represented as strings containing four octets (bytes), each separated by a dot (.). To compute a network ID, each octet in the IP address must be ANDed with each corresponding octet in the network mask.

Callout A in Listing 1 shows the VBScript NetworkID() function, which takes an IP address and network mask string and returns the network ID. To do this, the function uses VBScript's Split() function to convert both the IP address and network mask into arrays. The NetworkID() function also uses the ReDim statement to create a Result network ID array. Using a For ... Next statement, the NetworkID() function ANDs each octet in the IP address with the corresponding octet in the mask, and places the result in the Result array. Last, the function uses VBScript's Join() function to return the Result array as a single string, separating the elements in the array with a dot. Callout A in Listing 2 shows an equivalent network ID–finding function written in JScript.

Now that we have a function for computing a network ID based on an IP address and network mask, we can turn to the task of using WMI to retrieve the IP address and network mask from a computer's network adapter.

Retrieving a computer's IP address. The first step in retrieving a computer's IP address is to query WMI for IP-enabled adapters. Then we retrieve the arrays containing the adapter's IP address(es) and network mask(s). Assuming the computer has a single adapter with a single IP address, we can use the first item in both arrays.

Callout B in Listing 1 shows the VBScript ComputerNetID() function. It takes a computer name as input, uses WMI to retrieve the computer's IP address and network mask, and uses the NetworkID() function to calculate the computer's network ID. The first thing that the ComputerNetID() function tries to do is connect to the specified computer by using WMI. If this fails, the function returns "0.0.0.0". If the WMI connection succeeds, it uses WMI's ExecQuery() method to retrieve a collection of IP-enabled network adapters. ComputerNetID() iterates this collection by using For ... Next, looking for the first adapter that has non-null IPAddress and IPSubnet properties. ComputerNetID() retrieves the first IP address and network mask from both arrays and uses the NetworkID() function to calculate the network ID. ComputerNetID() can connect to the local computer by specifying a dot for its parameter. Also note that the script operator must have sufficient access to the named computer. (Callout B in Listing 2 shows an equivalent JScript function.)

The ComputerNetID() function uses the first IP address and network mask bound to the first Win32_NetworkAdapterConfiguration instance on the computer—the assumption being that this IP address and network mask accurately represent the computer. Be sure to test this code before using it with production systems where this assumption isn't valid. In general, though, NetID.vbs and NetID.js provide a simple way to determine a computer's network ID based on its IP address and network mask.