Introduction
In my article Restarting the web
server from your program, I introduced a simple C++ program that can
shutdown and restart the IISAdmin service. Actually, it is a more
general tool which can be used to install, uninstall, start, and stop any
windows service, not just services related to the web server. As pointed
out by some readers, we can also, maybe it is better to, use the VB scripts
provided by Microsoft to shutdown and restart web servers. If you are
using Windows 2000, you can find such VB scripts in the directory
\\Inetpub\AdminScripts. Those scripts are very powerful but not
exactly easy to use.
Details
Typically, you use the Internet Service Manager to start and stop web servers
interactively. You can also create new websites and new virtual web
directories from the Internet Service Manager. At my current job I have no
control over either the (formal) test environment or the production environment.
The web projects I worked on have to be installed and configured by
running a single batch file. The batch file will be run by our
administrator on the target machine to deploy the whole app. Here I
describe some VB script programs I wrote that can do the following
- Create a new website.
- Create a new virtual web directory.
- Start or stop a website.
My programs are modified from the AdminScripts and they do not provide
the full power and flexibility as the original scripts. Therefore they
will not satisfy everyone of your requirements. My main goal is to make
them easy to use for people not familiar with ADSI. The programs
have only been tested on Windows 2000, by the way.
First, let's see how
to create a new website. Oddly enough, a website on a particular machine
is not uniquely identified by a descriptive name string or the port number it is
bound to, but by a stupid index number you cannot see. So you can have two
websites on the same machine that look exactly the same when viewed from the
Internet Service Manager. Since you cannot run two websites that
use the same port number on the same machine simultaneously, I feel it is not a
big deal to assume in my script program that you won't create two websites that
use the same port. Here is the command to create a new website that is
bound to port 180.
CreateWebSite.vbs MyWebSiteName C:\MyWebRootDirectory 180
As you can see, the first parameter is a name string. The second
parameter is the full path of a physical directory chosen as the website root.
The last parameter is the port number used by the new website. If
there is already a website on your machine that uses port 180 or there is an
error, you will get an error-message box.
To create a new virtual web
directory on a website, you run the following command
CreateWebDir.vbs MyWebDirName C:\TheLocalDirectory 180
The first parameter is the name of the virtual directory, the second
parameter is the full path of a physical directory, and the third parameter is
the port number used by the website on which the new virtual web directory will
reside. You can also add a fourth parameter to specify the parent
virtual directory name if the parent is not the root but a subdirectory of
the root. If there is no website bound to port 180 or there is an error,
you will get an error-message box. By the way, if a virtual directory with
given name already exists on the corresponding website, the above command will
delete it before creating a new one.
Finally, the following command will
start the website on port 180
RunWebSite.vbs -r 180
You will get an error-message box if there is no website bound to port 180 on
your machine. A new website will be started by default when it created
using the command described earlier. To stop a website on port 180, use
the following command
RunWebSite.vbs -k 180
Here the -k option means kill and the -r option means
run.
If you need to set more options when creating and running
websites, you can modify the above programs yourself. For example, if you
don't want to give the user directory browsing capability, just comment out the
line oNewDir.EnableDirBrowsing = True
from the
CreateWebDir.vbs file. If you are not as lazy as me, you may want
to learn how to use the AdminScripts directly or write better programs to
do this. For detailed help, look for the IIsWebService, the
IIsWebServer, and the IIsWebVirtualDir objects in MSDN.
Thank you.