Home > IIS 6, Windows > Create Virtual Directory in IIS 6.0 via Command line

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
:: ****************************************

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

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

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

:: ========================================
::   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.

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

:: ========================================
::   STEP 6: ASSIGN APPLICATION TO VIRDIR
:: ========================================
echo Assigning Application Pool to Virtual Directory . . .
echo ------------------------------------------------------------
CSCRIPT //nologo %dir%\adsutil.vbs SET w3svc/1/Root/%var%/AppPoolid %var%
echo.
PAUSE

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.
01.IISMgr

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

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

Looking into the details

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

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

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).
06.VerifyDefaultDocs

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

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

About these ads
  1. July 20, 2011 at 6:45 am

    Great script ..good work

  2. Bojan
    August 30, 2011 at 4:24 am

    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 !

  3. Jayaseelan Vijayan
    September 19, 2011 at 2:01 am

    Gr8 work… i loved it…

  4. KingCoder
    November 1, 2011 at 4:24 am

    Brilliant – I love people who take the time to share. Keep it up – a definite time saver for me.

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: