Updating several IP addresses using ADSI
Like many web programmers, I host several hobby web sites for fun. (They make a wonderful test bed for new code. ;-] )
And like many computer enthusiasts, I sometimes change my ISP for one reason or another. If you are hosting web sites in a similar situation, I'm sure that you can identify the pain of trying to manually update each old IP address to your new IP address. This situation can be made even more difficult when any number of your web sites are using several host headers because the user interface for the IIS 6.0 administration tool only lists the first host header. This means that you have to manually view the properties for every site just to locate the IP addresses that you are required to change.
Well, I'm a big believer in replacing any repetitive task with code when it is possible, and a recent change of ISP provided just the right level of inspiration for me to write a simple Active Directory Service Interfaces (ADSI) script that locates IP addresses that have to be changed and updates them to their new values.
To use the example script, I would fist suggest that you make a backup copy of your metabase. (The script works fine, but it is always better to have a backup. ;-] ) As soon as your metabase has been backed up, copy the example script into notepad or some other text editor, update the old and new IP addresses that are defined as constants, and then run the script.
Option Explicit On Error Resume Next Dim objIIS Dim objSite Dim varBindings Dim intBindings Dim blnChanged Const strOldIP = "10.0.0.1" Const strNewIP = "192.168.0.1" Set objIIS = GetObject("IIS://LOCALHOST/W3SVC") If (Err <> 0) Then WScript.Echo "Error " & Hex(Err.Number) & "(" & _ Err.Description & ") occurred." WScript.Quit Else For Each objSite In objIIS blnChanged = False If objSite.class = "IIsWebServer" Then varBindings = objSite.ServerBindings For intBindings = 0 To UBound(varBindings) If InStr(varBindings(intBindings),strOldIP) Then blnChanged = True varBindings(intBindings) = Replace(varBindings(intBindings),strOldIP,strNewIP) End If Next End If If blnChanged = True Then objSite.ServerBindings = varBindings objSite.Setinfo End If Next End If MsgBox "Finished!"
That's all for now. Happy coding!