Configure Alternate IP (VBScript)

This code can be found in Chapter 1 of Windows Server 2003 Networking Recipes

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Windows Server 2003 Networking Recipes"

' This code configures the Registry blob that enables
' Alternate IP Configuration on a particular NIC.

' Grab the GUID for the appropriate NIC at the command line
' using the following syntax:
' > wmic nicconfig get ipaddress,settingid > \foo.txt
' > for /f "tokens=2" %a in ('type foo.txt ^| findstr "<IP Address>"') do echo %a

' ------ SCRIPT CONFIGURATION ------
  Const CONNECTED = 2
  Const HKEY_LOCAL_MACHINE  = &H80000002

  strTargetGUID = "{01B3816C-AB47-3E53-CB7C-88345293465}"
  strAlternateIP = "192.168.1.151"
  strAlternateMask = "255.255.255.0"
  strAlternateGW = "192.168.1.1"
  strAlternateDNS1 = "192.168.1.120"
  strAlternateDNS2 = "192.168.1.121"

  Const strComputer = "."

' ------ END CONFIGURATION ---------

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set nics = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each nic in nics
  strGUID = nic.SettingID

  ' only populate the alternate IP information for the correct NIC
  If strGUID = strTargetGUID Then

    ' make sure that DHCP is enabled
    If nic.DHCPEnabled = False Then
      Wscript.Echo("Error! DHCP must be enabled for " _
        & "alternate IP configurations to function.")

    ' now you can get to work
    Else
      ' first enable alternate IP configuration for this NIC
      strPath = "SYSTEM\CurrentControlSet\Services\" _
        & "Tcpip\Parameters\Interfaces\" & strGUID
      strValue = "ActiveConfigurations"
      strRegValue = "Alternate_" & strGUID
      arrValues = Array(strRegValue)

      Set Registry = GetObject _
       ("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")
      Registry.SetMultiStringValue HKEY_LOCAL_MACHINE, strPath, _
        strValue, arrValues

      ' now populate the alternate config with the appropriate values
      ' the first 20 values of the blob are fixed
      arrBlobValues(0)  = &H32
      arrBlobValues(1)  = &H00
      arrBlobValues(2)  = &H00
      arrBlobValues(3)  = &H00
      arrBlobValues(4)  = &H00
      arrBlobValues(5)  = &H00
      arrBlobValues(6)  = &H00
      arrBlobValues(7)  = &H00
      arrBlobValues(8)  = &H04
      arrBlobValues(9)  = &H00
      arrBlobValues(10) = &H00
      arrBlobValues(11) = &H00
      arrBlobValues(12) = &H00
      arrBlobValues(13) = &H00
      arrBlobValues(14) = &H00
      arrBlobValues(15) = &H00
      arrBlobValues(16) = &HFF
      arrBlobValues(17) = &HFF
      arrBlobValues(18) = &HFF
      arrBlobValues(19) = &H7F

      ' next insert the 4 octets of the IP address into
      ' array index 20 – 23
      arrIP = Split(strAlternateIP, ".")
      index = 20
      For Each octet in arrIP
        arrBlobValues(index) = CInt(octet)
        index = index + 1
      Next

      ' the next 20 values of the blob are fixed
      arrBlobValues(24) = &H01
      arrBlobValues(25) = &H00
      arrBlobValues(26) = &H00
      arrBlobValues(27) = &H00
      arrBlobValues(28) = &H00
      arrBlobValues(29) = &H00
      arrBlobValues(30) = &H00
      arrBlobValues(31) = &H00
      arrBlobValues(32) = &H04
      arrBlobValues(33) = &H00
      arrBlobValues(34) = &H00
      arrBlobValues(35) = &H00
      arrBlobValues(36) = &H00
      arrBlobValues(37) = &H00
      arrBlobValues(38) = &H00
      arrBlobValues(39) = &H00
      arrBlobValues(40) = &HFF
      arrBlobValues(41) = &HFF
      arrBlobValues(42) = &HFF
      arrBlobValues(43) = &H7F

      ' now insert the 4 octets of the subnet mask
      ' into array index 44 – 47
      arrIP = Split(strAlternateMask, ".")
      index = 44
      For Each octet in arrIP
        arrBlobValues(index) = CInt(octet)
        index = index + 1
      Next

     ' now insert another 20 fixed values
      arrBlobValues(48) = &H03
      arrBlobValues(49) = &H00
      arrBlobValues(50) = &H00
      arrBlobValues(51) = &H00
      arrBlobValues(52) = &H00
      arrBlobValues(53) = &H00
      arrBlobValues(54) = &H00
      arrBlobValues(55) = &H00
      arrBlobValues(56) = &H04
      arrBlobValues(57) = &H00
      arrBlobValues(58) = &H00
      arrBlobValues(59) = &H00
      arrBlobValues(60) = &H00
      arrBlobValues(61) = &H00
      arrBlobValues(62) = &H00
      arrBlobValues(63) = &H00
      arrBlobValues(64) = &HFF
      arrBlobValues(65) = &HFF
      arrBlobValues(66) = &HFF
      arrBlobValues(67) = &H7F

      ' now insert the 4 octets of the default gateway
      ' into array index 68 – 71
      arrIP = Split(strAlternateGW, ".")
      index = 68
      For Each octet in arrIP
        arrBlobValues(index) = CInt(octet)
        index = index + 1
      Next

      ' 20 more fixed values
      arrBlobValues(72) = &H06
      arrBlobValues(73) = &H00
      arrBlobValues(74) = &H00
      arrBlobValues(75) = &H00
      arrBlobValues(76) = &H00
      arrBlobValues(77) = &H00
      arrBlobValues(78) = &H00
      arrBlobValues(79) = &H00
      arrBlobValues(80) = &H08
      arrBlobValues(81) = &H00
      arrBlobValues(82) = &H00
      arrBlobValues(83) = &H00
      arrBlobValues(84) = &H00
      arrBlobValues(85) = &H00
      arrBlobValues(86) = &H00
      arrBlobValues(87) = &H00
      arrBlobValues(88) = &HFF
      arrBlobValues(89) = &HFF
      arrBlobValues(90) = &HFF
      arrBlobValues(91) = &H7F

      ' now insert the 4 octets of the primary DNS server
      ' into array index 92 – 95
      arrIP = Split(strAlternateDNS1, ".")
      index = 92
      For Each octet in arrIP
        arrBlobValues(index) = CInt(octet)
        index = index + 1
      Next

      ' now insert the 4 octets of the secondary DNS server
      ' into array index 96 – 99
      arrIP = Split(strAlternateDNS2, ".")
      index = 96
      For Each octet in arrIP
        arrBlobValues(index) = CInt(octet)
        index = index + 1
      Next

      ' finally, save this information to the Registry
      strPath = "SYSTEM\ControlSet001\Services\Dhcp\Configurations\Alternate_" _
        & strGUID
      strValue = "Options"

      Set Registry = GetObject _
       ("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")

      Return = Registry.CreateKey(HKEY_LOCAL_MACHINE,strPath)
   
      Return = Registry.SetBinaryValue(HKEY_LOCAL_MACHINE,strKeyPath,_
        strValue,arrBlobValues)
    End If
  End If
Next
WScript.Echo "Script completed successfully. "

This code has been viewed 3824 times.

New from the creators of TechTasks.com: StatSheet.com