Creating a website from the command line

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

  1. Create a new website.
  2. Create a new virtual web directory.
  3. 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.

Xiangyang Liu


Click here to view Xiangyang Liu's online profile.


Other popular articles:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值