Ever had a need to automate the creation of virtual directories in IIS? Coldfusion 8 can do this quite well using the System.DirectoryServices.DirectoryEntry class in the .NET framework.
After a whole lot of trial and error, this is what I came up with and it seems to work quite nicely:
<!--- get the server ID for the specific web site
from IIS. The default web site is usually "1"
--->
<cfset serverid="2017647531">
<!---
// metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
// for example "IIS://localhost/W3SVC/1/Root"
--->
<cfset metabasePath = "IIS://localhost/W3SVC/#serverid#/Root">
<!--- name of the virtual directory --->
<cfset vdirName = "vdir001">
<!--- physical path for this virtual directory --->
<cfset physicalPath = "D:\Inetpub\ricktest\realdir">
<!--- full path to the System.DirectoryServices.dll --->
<cfset dllPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.DirectoryServices.dll">
<!--- instantiate a DirectoryEntry .NET object --->
<cfobject
type=".net"
class="System.DirectoryServices.DirectoryEntry"
assembly="#dllPath#"
name="site">
<!--- initialize the object with the metabase path --->
<cfset site.init(metabasePath)>
<!--- you need the classname for later --->
<cfset className = site.Get_SchemaClassName()>
<cfif reFind("VirtualDir$",className) gt 0>
<!--- Get the children so we can add a new child --->
<cfset vdirs = site.Get_Children()>
<cftry>
<!--- add the new virtual directory --->
<cfset newVDir = vdirs.Add(vDirName, className)>
<cfcatch type="any">
<cfoutput><h3>Failed to create #vDirName#</h3></cfoutput>
<cfif cfcatch.ToString() contains "already exists">
<p>Another directory with that name already exists.</p>
<cfelse>
<cfoutput><p>#cfcatch.ToString()#</p></cfoutput>
</cfif>
<cfabort>
</cfcatch>
</cftry>
<Cftry>
<!--- set the PAth property to the physical directory path --->
<cfset newVDir.Get_Properties().Get_Item("Path").Set_Value(physicalPath)>
<!--- commit the changes --->
<cfset newVDir.CommitChanges()>
<cfcatch type="any">
<cfdump var="#cfcatch.ToString()#">
<cfdump var="#cfcatch#">
<Cfabort>
</cfcatch>
</cftry>
</cfif>
