Steve Bryant have today a nice post about how to create IIS Web sites from ColdFusion. I wanted to play with it myself in the weekend and Steve's solution came more than in a good moment. Very nice solution. However at least for me and my configuration it didn't work exactly as i would like to. I am talking about the following line:
<cfset var IP = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress()>
For me it made to be the local IP address, so not quite what i would like it to work like.
So i made a couple of small changes to Steve's function.
First, i added a new argument:
<cfargument name="ip" type="string" default="">
so that we to can pass the particular IP if we want to.
Another piece of code was replacing
<cfset var IP = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress()>
by
<cfif Len(Trim(IP)) eq 0>
   <cfset IP = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress()>
<cfelseif IP eq "All">
   <cfset IP = "">
</cfif>
This will do the following:
  1. If no IP is provided, will be used CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress() for that matter
  2. If IP is passed as "All", the site in IIS will be created for "All Unassigned" and so it will work for any IP pointed to that server
  3. If the IP is passed, that code will be skipped and provided IP will be the one used for the new site in IIS
And here is the final code:
<cffunction name="addIISWebSite" access="public" returntype="any" output="false" hint="">
   <cfargument name="destination" type="string" required="yes">
   <cfargument name="SiteName" type="string" required="yes">
   <cfargument name="domains" type="string" required="yes">
   <cfargument name="ip" type="string" default="">
   
   <cfset var AddSiteVBS = "">
   <cfset var AddSiteFile = "#arguments.destination#addsite.vbs">
   <cfset var BatchFile = "#arguments.destination#addsite.bat">
   <cfset var BatchCode = "cscript #arguments.destination#addsite.vbs">
   <cfset var ii = 0>
   <cfset var domain = "">
   <cfset var StartSiteVBS = "">

   <cfif Len(Trim(IP)) eq 0>
      <cfset IP = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostAddress()>
   <cfelseif IP eq "All">
      <cfset IP = "">
   </cfif>

<cfsavecontent variable="AddSiteVBS"><cfoutput>
' Make connections to WMI, to the IIS namespace on MyMachine, and to the Web service.
strComputer = "."
set locatorObj = CreateObject("WbemScripting.SWbemLocator")
set providerObj = locatorObj.ConnectServer(strComputer, "root/MicrosoftIISv2")
set serviceObj = providerObj.Get("IIsWebService='W3SVC'")

' Build binding object, which is a required parameter of the CreateNewSite method.
' Use the SpawnInstance WMI method since we are creating a new instance of an object.
Bindings = Array(#ListLen(arguments.domains)-1#)<cfloop from="1" to="#ListLen(arguments.domains)#" index="ii" step="1"><cfset domain = ListGetAt(arguments.domains,ii)>
Set Bindings(#ii-1#) = providerObj.get("ServerBinding").SpawnInstance_()
Bindings(#ii-1#).IP = "#IP#"
Bindings(#ii-1#).Port = "80"
Bindings(#ii-1#).Hostname = "#domain#"
</cfloop>
' Create the new Web site using the CreateNewSite method of the IIsWebService object.
Dim strSiteObjPath
strSiteObjPath = serviceObj.CreateNewSite("#arguments.SiteName#", Bindings, "#arguments.destination#")
If Err Then
WScript.Echo "*** Error Creating Site: " & He<img src="images/smiles/14.gif" border="0">Err.Number) & ": " & Err.Description & " ***"
WScript.Quit(1)
End If

' strSiteObjPath is in the format of IIsWebServer='W3SVC/1180970907'
' To parse out the absolute path, W3SVC/1180970907, use the SWbemObjectPath WMI object.
Set objPath = CreateObject("WbemScripting.SWbemObjectPath")
objPath.Path = strSiteObjPath
strSitePath = objPath.Keys.Item("")

' Set some properties on the root virtual directory which was created by CreateNewSite.
Set vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" & strSitePath & "/ROOT'")
vdirObj.AuthFlags = 5 ' AuthNTLM + AuthAnonymous
vdirObj.EnableDefaultDoc = True
vdirObj.DirBrowseFlags = &H4000003E ' date, time, size, extension, longdate
vdirObj.AccessFlags = 513 ' read, script
vdirObj.AppFriendlyName = "#arguments.SiteName#"

' Save the new settings to the metabase
vdirObj.Put_()

' CreateNewSite does not start the server, so start it now.
Set serverObj = providerObj.Get(strSiteObjPath)
serverObj.Start

WScript.Echo "A New site called #arguments.SiteName# was created with the path and unique site identification number of " & strSitePath
</cfoutput></cfsavecontent>

   <cffile action="WRITE" file="#AddSiteFile#" output="#AddSiteVBS#">
   <cffile action="WRITE" file="#BatchFile#" output="#BatchCode#">
   
   <cfexecute name="#BatchFile#" timeout="999" />
   
   <cffile action="DELETE" file="#BatchFile#">
   <cffile action="DELETE" file="#AddSiteFile#">
</cffunction>
And the call to the function would may look something like:
<cfset addIISWebSite(destination="E:\Projects\newsite\", SiteName="mydomain.com", domains="mydomain.com", IP="All")>

Thx again Steve.