Delen via


Reading all WINS IP addresses on a computer using Windows Scripting Host ( VBScript )

I recently worked with a developer who was working on a VBScript to enumerate all Windows Internet Name Service (WINS) IP addresses of a computer using the Win32_NetworkAdapterConfiguration WMI class.

The developer was only able to enumerate the first two WINS IP addresses of a computer. The Win32_NetworkAdapterConfiguration class uses two properties to read WINS addresses, i.e. WINSPrimaryServer and WINSSecondaryServer. which reads the Primary and Secondary WINS IP Address, however, if there are more than 2 WINS IP Address specified on a computer, they are not stored in any of the properties.

After much research, we found that under the hood, the Win32_NetworkAdapterConfiguration WMI class uses GetAdaptersAddressess API to read the WINS IP addresses. Although the API retrieves all WINS IP Addresses, the class was designed to return only the first two of items. Because the GetAdaptersAddressess API is C++ function, it is not directly available in scripting, however, the developer wanted a solution in VBScript.

While researching to find out an alternate way to read all WINS IP addresses in VBScript, I was able to find a registry key which corresponds to WINS IP Addresses of a computer. The NameServerList registry key found in the registry path below, contains all the WINS IP Addresses of a computer. The key will have the form of the following:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces\Tcpip_{A4FD1AB7-DC25-48CD-AFE2-899EC36AF517}

You may have multiple keys of this form, each representing the TCPIP settings for a specific installed network adapter. The following KB provides a bit more detail on the registry values for TCPIP that are associated with a given adapter:

support.microsoft.com/kb/120642

The NameServerList is a MULTISTRING registry key value which lists all the WINS IP addresses. This registry key is empty if there are no WINS IP addresses in the computer. Whenever we add a WINS IP address into LAN Properties this registry key will be updated with the added IP address.

technet.microsoft.com/en-us/library/cc776758(v=WS.10).aspx

I wrote a separate VBScript application which uses StdRegProv to read all WINS addresses from NameServerList. The VBScript application is then incorporated in the customer’s main application to successfully read all the WINS IP addresses. Furthermore, added a few lines of code into VBScript to modify the value of NameServerList which then modifies WINS IP address list of the computer

A sample of VBScript application to read all WINS IP addresses of a computer from NameServerList registry key is pasted below 

   1: Option Explicit
   2: 
   3: const HKEY_LOCAL_MACHINE = &H80000002
   4: 
   5: Dim objReg
   6: Dim strComputer, strKeyPath, strTemp, strSubkey, strMulArrVal  
   7: Dim arrValueNames, arrValueTypes, arrValues, arrVal, arrSubkeys
   8: Dim iKeyExist, iLoopVal, iReturn
   9: 
  10: ' Binds to Local computer/machine 
  11: strComputer = "."
  12: ' Connect to stdRegProv class 
  13: Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_ 
  14:     strComputer & "\root\default:StdRegProv")
  15: 
  16: ' Registry key path hardcoded
  17: strKeyPath = "SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces"
  18: ' Enumerate subkeys of the strKeyPath
  19: iKeyExist = objReg.EnumKey(HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys) 
  20: 
  21: 'Check whether the reg key exists (If not, returns 2)
  22: If iKeyExist = 0 Then
  23:     'Enumerate each subkey
  24:     If IsArray(arrSubkeys) Then 
  25:         For Each strSubkey In arrSubkeys
  26:             ' Check if subkey is a Tcpip_{...}
  27:             If (strComp(Left(strSubkey,6),"Tcpip_") = 0) then
  28:                 WScript.Echo vblf & "WINS Set of " & strSubkey
  29:                 ' Frame the path of the subkey - Example: "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces\Tcpip_{A4FD1AB7-DC25-48CD-AFE2-899EC36AF517}"
  30:                 strTemp = strKeyPath & "\" & strSubkey 
  31:                 'Enumerate the key values of strTemp (subkey)
  32:                 objReg.EnumValues HKEY_LOCAL_MACHINE,strTemp,arrValueNames,arrValueTypes
  33:                 If Not IsNull(arrValueTypes) Then
  34:                     For iLoopVal=0 To UBound(arrValueNames)
  35:                         ' Check for NameServerList which has WINS addresses
  36:                         If strComp(arrValueNames(iLoopVal),"NameServerList") = 0 Then
  37:                             ' NameServerList is a multistring REG 
  38:                             ' GetMultiStringValue method to read all WINS IP addresses 
  39:                             iReturn = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strTemp,arrValueNames(iLoopVal),arrValues)
  40:                             If (iReturn = 0) And (Err.Number = 0) Then   
  41:                                 WScript.Echo "Total number of WINS IP addresses: " & UBound(arrValues) + 1                            
  42:                                 For each arrVal in arrValues
  43:                                     WScript.Echo arrVal
  44:                                 Next
  45:                             End IF
  46:                         End If 
  47:                     Next
  48:                 End If
  49:             End IF
  50:         Next
  51:     End If 
  52: End If
  53: 

Below is a sample code that illustrates how to modify WINS IP addresses of a machine through NameServerList

   1: strMulArrVal = Array("111.111.111.111","111.111.111.111","111.111.111.111") ' Array - valid code 
   2: strTemp = "SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces\Tcpip_{A4FD1AB7-DC25-48CD-AFE2-899EC36AF517}"
   3: objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strTemp,"NameServerList",strMulArrVal ' set method - valid code
   4: