11. January 2006
IIS , Scripting
(Note: I had originally posted this information on a blog that I kept on http://weblogs.asp.net, but it makes more sense to post it here. [:)] )
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 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 first 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.
On Error Resume Next
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."
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)
If blnChanged = True Then
objSite.ServerBindings = varBindings
That’s all for now. Happy coding!
Note: This blog was originally posted at http://blogs.msdn.com/robert_mcmurray/