Many of us do work for many different companies in addition to our own hobby sites and the favors we do for friends. Each of these projects can require their own configurations, virtual directories or overlapping script files. As a developer it is crucial to be able to switch configurations and to create folder structures that mimic our clients production environments.
In order to accomplish this task we must first analyze the difference between IIS in windows professional and sever versions. Is there really any difference at all? I don’t know. But I do know that the restriction on virtual servers is deliberate, and it is implemented in two parts. 1) The administration tool doesn’t have an option for creating a website. 2) if you try to start a web site while another one is active it raises an error.
While there are times when you may need to have two configurations running at the same time and for those situations you will need to install a copy of ‘server’. I am writing about the rest of the time, when the goal is having two configurations that you can switch between quickly and easily.
Using the code
The first step is to be able to create additional sites in IIS. For that I use the ADSI IIS objects which can be referenced through the
GetObject function like this
GetObject("IIS://localhost/w3svc"). The w3svc object is a COM object which has properties and methods which can in turn return objects like any other object. One of the methods of that object is
Create. Which I use to create a new site like this
Websvr is the object returned from
2 is the ordinal number of the new site, or the amount of site already on the machine plus 1. The one last step in code is to save you changes with
NewSite is the object returned from create. At this point we have gotten past the missing command in the MMC snap in. you should now see the new site in the MMC snap in. it will have a stop sign instead of the globe, but that is just because it doesn’t have a root directory. That an all other settings can now be set through the graphical interface. You could just continue the script to set all the settings you want but once we’ve gotten past the impossible I like to switch back to windows.
However there is one more useful script that I would like to share. Once the site is set up you still can not run two sites at once. You could technically open MMC every time you wanted to switch and shut down the active site and start the one you want. Much too much work and time the way MMC runs. So I wrote a script which I keep in my Quick Launch Toolbar to do the switch for me. I use ADSI once again for this task.
Dim websvr1, websvr2, websvr3, newSite, WhichSite
WhichSite = inputBox("Which Site would you like to use?",
"IIS site switch Utility","Chabad.org")
Set Websvr1 = GetObject("IIS://localhost/w3svc/1")
Set Websvr2 = GetObject("IIS://localhost/w3svc/2")
Set Websvr3 = GetObject("IIS://localhost/w3svc/3")
Select Case lcase(WhichSite)
Case "1", "local"
msgBox "The local-Default Web Site has been started."
Case "2", "msdn"
msgBox "The MSDN Web Site has been started."
Case "3", "codeproject"
msgBox "The CodeProject Web Site has been started."
MsgBox "Sorry we do not have a website set up with that name."
I first ask my self which site to turn on and then I use
GetObject once again to get a reference to every site running on the computer. Shut them all down. And finally turn on the one I requested.
If you wanted to make this into a lightweight application (if such a thing exists) or if you wanted a drop down list to chose from you could just implement the same solution in VB 6, VB.NET or C#.