Switching an IIS website home directory from the command line

The title says it all.

The problem was simple – we have a legacy .NET 1.1 website project, and have several branches for each environment it’s currently running in. In order to test bug fixes for a branched version, I always find myself needing to go into IIS management console and switch the website to the directory of the branch. A reasonably easy step, but always cumbersome because it takes focus off solving the problem at hand.

What i wanted was to create a batch script i could execute, and IT would be clever enough to update the IIS website to the correct directory.

Searching for command-line management tools for IIS turned up a lot of results, but none of them mention anything about modifying an existing website. They all have functionality to query/add/delete a website from the IIS metabase, but nothing to modify an existing one.

After a bit more digging, i was lucky enough to find this article explaining how to modify an IIS website path using Active Directory Service Interfaces (ADSI). It’s a small C# code snippet, but was enough for me to write a console-app to do exactly what I needed.

The only thing that the utility did not mention was that it relies on the input id being the IIS metabase website id, and not the name of the website. But determining the id of the website from the name was a trivial call to the iisweb.vbs script, and then a regex to pull out the ID from the output

%SystemRoot%System32iisweb.vbs /query “My Website”

Once the app was written, I knocked up a quick batch script to perform the switch (with a little refresher on my ms-dos syntax):

@echo off
echo Switching My Website to directory %CD%

echo First lets stop IIS (unnecessary, but being defensive here)
iisreset /stop

.toolsswitchiisswitchiis.exe “My Website” “%CD%MyWebDir”

echo Restarting IIS…..
iisreset /start

pause

In case people are interested, i’ve zipped and attached the project (assembly and code). It’s not robust, and it could be better, but its a tool for a specific job. YMMV.

Download SwitchIIS

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *