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.

Advertisements

Checking Permissions with Batch Process

There is no denying it… I am a die-hard command-line junkie. When deploying to new systems I have my batch file arsenal with me at all times strictly because it keeps my processes very procedural and standardized. There are times when I come into an environment where the systems have already been deployed. Which makes things a bit challenging especially when dealing with multiple profiles on a single machine. Because of the fact that most of my deployments require SQL Server Express I have batch files setup accordingly that specifies the needed firewall exceptions along with other added automated steps. Just to name a few.

When dealing with existing systems I try to make dual processes 1) when I have admin access and 2) when I don’t have admin access. This all depends on the logged-on user of course. So in order to preserve the natural order of life I do my best to not interfere with user’s settings because it tends to throw them off and generally upsets them. So I keep their environment pretty much intact. In order to maintain consistency I run my dual processes. Not ideal but it works. Of course there are instances where I absolutely require admin access, but for the little items that are merely file placement and such I get by with the separate process.

It sounds like a lot of work, but once you have your batch file laid out the rest is cake. The first thing I do is check to see if the user has elevated privileged. I find trying to query the registry works well in indicating your permission level.

reg query "HKU\S-1-5-19" >NUL
echo %errorlevel%

If I ran this under a user account that is in the Users or Power Users group then the errorlevel will return a value of one.

01.StandardUser

If ran using an account with admin privledges you will see a value of zero returned.

02.AdminUser

The next step is to define the process to execute once the permission level has been determined. This is easily directed with the goto statement. Let’s take a look.

@echo off
color 17
reg query "HKU\S-1-5-19" >NUL
CLS
If Not %errorlevel% == 0 goto UserDeploy
If %errorlevel% == 0 goto AdminDeploy

:UserDeploy
::Run this process
goto end

:AdminDeploy
::Run this process
goto end

:end

I added the CLS on line two to clear the “Error: Access is denied.” message that is displayed on the first screenshot.

CLS

In terms of the dual processes I setup self-extracting zips to deploy to certain directories based on privileges.