Scripting IIS: Change Web Root

I recently had a need to be able to set the IIS web root from ColdFusion. This is for development on a local machine (where IIS can have only one web root). To do this, I took advantage of the load_iis.bat that my friend Jason Holden created.

I use ColdFusion to write and execute that file in the location that I want to use as the IIS web root.

Here is the function:

<cffunction name="makeIISRoot" access="public" returntype="any" output="false" hint="">
<cfargument name="destination" type="string" required="yes">

<cfset var BatchFile = "#arguments.destination#load_iis.bat">
<cfset var LoadIIS = 'cscript %SystemDrive%\inetpub\adminscripts\adsutil.vbs SET /W3SVC/1/ROOT/path "%~dp0"'>

<cffile action="WRITE" file="#BatchFile#" output="#LoadIIS#">

<cfexecute name="#BatchFile#" timeout="999" />

<cffile action="DELETE" file="#BatchFile#">

</cffunction>

It is pretty simple. I pass in the folder (including trailing path delimiter) to the function. The "LoadIIS" variable is the code for Jason's load_iis.bat file. That code uses adsutil.vbs that comes pre-installed in the adminscripts folder of inetpub. It calls the "SET" method and passes in an argument for the root of the web site and one that is a shortcut for the current path.

The function writes the .bat file into the root of the site, executes it with cfexecute and then deletes it.

I also could have created the file anywhere and replaced "%~dp0" with "#arguments.destination#" and that should work as well (though I haven't tested it).

With this, it would be pretty easy to write a page that allowed you to switch which site you have as your web root.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Greetings.
an interesting approach. However being limited to a CF based solution may not be the best. While it isn't scriptable, IISAdmin (http://www.firstserved.net/help/downloads) works very well, allowing you to define and switch sites on the fly. What's better is that its free.

Unfortunately the site is in dutch, but here's the link to the google translation:
http://tinyurl.com/6avxme

regards,
larry
# Posted By Larry C. Lyons | 11/14/08 9:23 AM
Larry,

Thanks for the comment.

Note, however, the first sentence of the entry "I recently had a need to be able to set the IIS web root from ColdFusion.". I'm aware it could be done other ways, but I needed to do so from ColdFusion and thought someone else might have that need as well.

Still, thanks for pointing to other resources as those might be helpful to someone else.
# Posted By Steve Bryant | 11/14/08 11:01 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.