IISChangeIP.vbs – Mass IP Address Change and Configuration Tool For IIS

I was recently tasked with changing the IP address for the host headers of an IIS 5 machine. This is a pretty common task as machines are physically relocated; sites are moved from IP to IP address or network to network.

Although this task is simplistic and pretty straight forward to do through the IIS Manager, this particular machine was unique in that it has 1943 host headers associated with nearly as many websites. Yes that is almost two thousand websites on one IIS machine on just 1 IP address. If I were to manually configure each header for the new IP address, and each host header took me 20 seconds to change, it would take me 38,860 seconds or 10.8 hours to change this one IP binding on this particular machine. More than 1½ days work just to configure one setting!

Since this was totally out of the question I decided to write a script (you’re shocked, I know) to handle this task for you instead. I have packaged this nicely for your IIS configuring pleasure.

This particular script works only with cscript.exe. This is the command line version of wscript.exe that I’m sure you’re probably familiar with if you’re reading this site. To use the script you can simply pass the IP address you’d like to change, and then the new IP address you’d like to change to. If you’d simply like to “unbind” the IP address so that the host headers use “All Unassigned” simply pass the keyword “unbound” in the place of an IP address.

I have borrowed some functions that I used in some previous scripts, one called IsIP, which simply verifies that the IP address you passed is a potentially valid IP, and CheckInterpreter which checks to see which VBScript interpreter you are using to execute the script with, since this script won’t work if you simply double click on it.

As always please feel free to respond with any questions or comments you might have.

'This script is provided under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com
'
'
' IISChangeIP.vbs
' Author: Nate Rice - http://www.naterice.com/
' Date: 09/18/2007
' Syntax: cscript.exe IISChangeIP.vbs <oldip> <newip>
'
' Purpose: This script is intended to be used with IIS to change the IP address of
' multiple sites. This script can change the bindings of all sites within IIS that
' are assigned to a specific IP or are "unbound". For example, if there is a site
' that is assigned to the IP 192.168.1.100 and you wish to change to to "All
' Unassigned" you could do that by passing the following command:
'
' cscript.exe iischangeip.vbs 192.168.1.100 unbound
'
' This script has been tested on IIS 5 and IIS 6.

Set oArgs = Wscript.Arguments

Dim aArgs()
ReDim aArgs(oArgs.Count)
x = 0
Do Until x = oArgs.Count
  aArgs(x) = oArgs(x)
  x=x+1
Loop

vTotalChangeCount = 0

If Not CheckInterpreter Then
  ErrorHandler(1)
Else
  If Ubound(aArgs) = 2 Then
    Wscript.StdOut.Write vbCRLF & "This will change all instances of " & aArgs(0) & _ 
    " in IIS to " & aArgs(1) & "." & vbCRLF & vbCRLF & "  Continue? (y/N): "
    sContinue = Wscript.StdIn.ReadLine
    If lcase(sContinue) = "y" Then
      IISChangeIP aArgs(1), aArgs(0)
      Wscript.Echo "Changed: " & vTotalChangeCount & " IPs"
    End If
  Else
    ErrorHandler(1)
  End If
End If


Sub IISChangeIP(sNewIP, sOldIP)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com
  'here we are doing basic checking to make sure that the IP's we passed are
  'actually IP's, that they are the keyword "unbound" and that we aren't setting
  'the original IP and the new IP to the same thing.
  If sNewIP = "unbound" Then
    sNewIP = ""
  ElseIf IsIP(sNewIP) Then
  
  Else
    ErrorHandler(2)
    Exit Sub
  End If
  If sOldIP = "unbound" Then
    sOldIP = ""
  ElseIf IsIP(sOldIP) Then
  
  Else
    ErrorHandler(3)
    Exit Sub
  End If
  If sOldIP = sNewIP Then Exit Sub
  
  On Error Resume Next
    Set oWWWServer = GetObject("IIS://localhost/w3svc")
    
    If Err.Number <> 0 Then
      ErrorHandler(4)
      Exit Sub
    End If
  On Error Goto 0

  For Each oWebSite In oWWWServer
    'Here we loop through each node in the /w3svc key. Some of these aren't
    'websites they are other IIS objects so we filter those out based on
    'whether or not they have log file directories. The ones that error are not
    'websites so we skip them.
    On Error Resume Next
    sLogFileDirectory = ""
    sLogFileDirectory = oWebSite.LogFileDirectory
    On Error Goto 0

    If Len(sLogFileDirectory) > 0 Then      
      aServerBindings = oWebSite.ServerBindings
      x=0
      
      'Here we loop throuch each header value, these are also known as
      '"bindings". Each binding has three values, the IP address it's
      'associated with, it's port, and then a host header value.
      For Each sBinding In aServerBindings
        Wscript.Echo "Original Binding: " & sBinding
        aBindings = Split(sBinding, ":")
        
        'Here we loop through each binding value and check the value.
        'blank values are checked as well as regular ips.
        For y=0 To Ubound(aBindings) Step 3
          If aBindings(y) = sOldIP Then
            Wscript.Echo "  - Found Match: " & aBindings(y)
            aBindings(y) = sNewIP
          End If
          y=y+3
        Next
        
        'This is the new binding string. If it's different than the original
        'we display a message noting that it has changed. We also
        'incriment the change counter.
        sNewBinding = aBindings(0) & ":" & aBindings(1) & ":" & aBindings(2)
        If Not sNewBinding = sBinding Then
          Wscript.Echo "  - New Binding: " & sNewBinding
          sBinding = sNewBinding
          vTotalChangeCount = vTotalChangeCount+1
        End If
        aServerBindings(x) = sBinding
        x=x+1
      Next
      
      'Here we set the new binding array to the server binding property
      'then we commit the values with the "setinfo" method.
      oWebSite.ServerBindings = aServerBindings
      oWebSite.SetInfo
    End If
  Next

  Set oWWWServer = Nothing
End Sub
  
Function CheckInterpreter
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  ' Checks to see if we should display error messages.
  ' If running under cscript echoing errors to shell is ok.
  ' If running under wscript echoing is NOT ok because
  ' echoing errors will pause execution of the script.

  sScriptHost = LCase(Wscript.FullName)

  If Right(sScriptHost, 11) = "wscript.exe" Then
    CheckInterpreter = False
  Else
    CheckInterpreter = True
  End If
End Function

Function IsIP(sIPAddress)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  'This script simply returns true or false if the string passed
  'can qualify as a real ip or not.
  aOctets = Split(sIPAddress,".")
 
  If IsArray(aOctets) Then
    If UBound(aOctets) = 3 Then
      For Each sOctet In aOctets
        On Error Resume Next
        sOctet = Trim(sOctet)
        sOctet = sOctet + 0
        On Error Goto 0
       
        If IsNumeric(sOctet) Then
          If sOctet < 0 Or sOctet > 256 Then
            IsIP = False
            Exit Function
          End If
        Else
          IsIP = False
          Exit Function
        End If
      Next
      IsIP = True
    Else
      IsIP = False
    End If
  Else
    IsIP = False
  End If
End Function

Sub ErrorHandler(vErrorNumber)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

    If vErrorNumber = 1 Then
      Wscript.Echo "This script must be run with cscript.exe." & vbCRLF & _
                   "Syntax: cscript.exe iischangeip.vbs <oldip> <newip>" & vbCRLF & _
                   vbCRLF & _
                   "Example usage: " & vbCRLF & _
                   "cscript.exe iischangeip.vbs unbound 192.168.1.111" & vbCRLF & _
                   "cscript.exe iischangeip.vbs 10.12.51.4 10.12.51.5"
    ElseIf vErrorNumber = 2 Then
      Wscript.Echo "ERROR: The value you passed for <newip> is not a valid IP" & _ 
                   " address or the keyword ""unbound"". Please recheck the ip" & _
                   " address and try again." & vbCRLF & vbCRLF & _
                   "Syntax: cscript.exe iischangeip.vbs <oldip> <newip>" & vbCRLF & _
                   vbCRLF & _
                   "Example usage: " & vbCRLF & _
                   "cscript.exe iischangeip.vbs unbound 192.168.1.111" & vbCRLF & _
                   "cscript.exe iischangeip.vbs 10.12.51.4 10.12.51.5"
    ElseIf vErrorNumber = 3 Then
      Wscript.Echo "ERROR: The value you passed for <oldip> is not a valid IP" & _
                   " address or the keyword ""unbound"". Please recheck the ip" & _
                   " address and try again." & vbCRLF & vbCRLF & _
                   "Syntax: cscript.exe iischangeip.vbs <oldip> <newip>" & vbCRLF & _
                   vbCRLF & _
                   "Example usage: " & vbCRLF & _
                   "cscript.exe iischangeip.vbs unbound 192.168.1.111" & vbCRLF & _
                   "cscript.exe iischangeip.vbs 10.12.51.4 10.12.51.5"
    ElseIf vErrorNumber = 4 Then
      Wscript.Echo vbCRLF & _
                   "ERROR: The script was unable to connect to IIS." & vbCRLF & _
                   "ERR Number: " & Err.Number & vbCRLF & _
                   "ERR Description: " & Err.Description
    End If
End Sub

 

3 thoughts on “IISChangeIP.vbs – Mass IP Address Change and Configuration Tool For IIS”

Leave a Comment