Create Virtual Directory in IIS 6.0 via Command line

Well for most of my implementations I deploy variations of Windows 2003 Server with x86 and x64 editions included. I always use IIS because I deploy web based applications so I like to keep things standardized as much as humanly as possible. Its just better that way. No arguments necessary.

Well in order to keep settings consistent across the board I need to ensure all of my IIS settings are intact. My implementations utilize virtual directories, but I also like to have a separate application pool associated to this virtual directory for obvious reasons. In the event you are not familiar: separating applications into their own application pool or group is called (isolation mode). This allows a site or a group of sites to be taken offline without affecting all sites that might reside on that server. If you have a application server that hosts many sites hopefully you have them configured using their own application pools.

In terms of default documents I like to specify only what I need which is typically (default.aspx) while removing everything else. Then I set the framework version to .NET 2.0. Of course I prefer to do this auto-magically to remove chances of human error so no manual labor for me I tell ya!

Below is what the contents of my batch file looks like. Feel free to use it if you find it helpful. Of course you want to test it on a test or dev environment first. It has worked for me several times but environments differ and you never know. Besides this particular script works if you have the inetpub on the C:\. (Notice Line 7)

Also note that this script is dependent on two VBS scripts (adsutil.vbs & IISvdir.vbs) both of which you will find on the Windows Server platforms and not on the desktops.

@echo off
color 17
Title My Standard IIS 6.0 Configuration . . .

:: ****************************************
SET var=MyApplicationName
SET dir=C:\Inetpub\AdminScripts
SET ito=120
SET dd=Default.aspx
:: ****************************************

:: ========================================
:: ========================================
echo Creating Application Pool . . .
echo ------------------------------------------------------------
CSCRIPT //nologo %dir%\adsutil.vbs CREATE w3svc/AppPools/%var% IISApplicationPool

:: ========================================
:: ========================================
echo Setting Idle Timeout . . .
echo ------------------------------------------------------------
CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/AppPools/%var%/IdleTimeout %ito%

:: ========================================
:: ========================================
echo Creating Virtual Directory . . .
echo ------------------------------------------------------------
CSCRIPT //nologo %systemroot%\system32\IISvdir.vbs /create "Default Web Site" "%var%" %systemdrive%\inetpub\wwwroot\%var%

:: ========================================
::   STEP 4: SET .NET VERSION to 2.0
:: ========================================
echo Setting .NET Version . . .
echo ------------------------------------------------------------
%windir%\Microsoft.Net\Framework\v2.0.50727\aspnet_regiis.exe -s w3svc/1/root/%var%

:: ========================================
:: ========================================
echo Setting Default Documents . . .
echo ------------------------------------------------------------
CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/1/Root/%var%/DefaultDoc %dd%

:: ========================================
:: ========================================
echo Assigning Application Pool to Virtual Directory . . .
echo ------------------------------------------------------------
CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/1/Root/%var%/AppPoolid %var%

Before we start running scripts let’s take a look at how IIS currently looks like. This way we can have a before and after comparison. So below is a screen cast of my demo system’s IIS. As you can see it is very clean because it’s a fresh install. I haven’t really done anything to it yet. Once we run the script we will see a newly created application pool and a newly created virtual directory.

Below is what the script looks like once it is executed on a server system that has IIS installed.

Now let’s verify the differences between the before and after. As you can see the Application Pool & Virtual Directory have been created…

Looking into the details

The default Idle Timeout is now 120 mins. In a typical install this is defaulted to 20 mins.

Virtual Directory details… this is pretty much the same but in this case it was configured via script.

Again… in a typical setup you would usually see (Default.htm, Default.asp, index.htm and iisstart.htm) listed as default documents. In this case we only specify what we really need and that is the (Default.aspx).

And lastly the Microsoft.NET Framework is set to v2.0 rather than v1.1…

As you can see this is relatively straightforward and helps me keep implementations consistent.

About these ads

4 thoughts on “Create Virtual Directory in IIS 6.0 via Command line

  1. Bojan says:

    Hello, I must say that this script you wrote does a fantastic job of IIS automatization.
    However, I am wondering is there a way to add a few other commands to set some settings like:

    1. Enabling parent paths for the created virtual directory/site, since they are disabled by default in IIS 6.0
    2. Set specific folders in the virtual directory to be applications of the newly created application pool
    3. Set permissions for some folders too.

    Do you maybe have an idea on how to do this, or some reference that could help in achieving this ?

    Thank you very much !

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s