Scripting the creation and configuration of IIS 7 websites
You can use Appcmd.exe and other commands to script the creation and configuration of IIS 7 websites. This approach is useful if you want to automate the creation and configuration of websites using batch files. It's also useful if you want to create and configure websites in an environment where you have Server Core deployed.
For example, to create a new website named New Site on a Server Core Web server named SEA-SC2, follow these steps:
Create a home directory for the site by using the mkdir command like this:
Create a default document in the home directory, for example by typing the following Copy command and then pressing Ctrl+Z as shown here:
copy con C:\newsite\default.htm
This is a test
Create the new site, assigning it a site ID of 2 and bindings of *:80:www.fabrikam.com, which indicates that the new site will listen on all IP addresses bound to the server’s network adapter, and on the default port 80, but only listens for HTTP requests that specify www.fabrikam.com as the host being requested, as follows:
appcmd add site /name:"New Site" /id:2 /physicalPath:C:\newsite /bindings:http//*:80:www.fabrikam.com
SITE object "New Site" added
APP object "New Site/" added
VDIR object "New Site/" added
1 file(s) copied.
Before your new site can respond to HTTP requests issued for www.fabrikam.com, you must create a new CNAME (alias) record in the fabrikam.com zone on your Domain Name System (DNS) server so that name queries issued for www.fabrikam.com will return the IP address assigned to SEA-SC2.fabrikam.com. To do this, use this command on your DNS server:
dnscmd SEA-DC2 /recordadd fabrikam.com www CNAME SEA-SC2.fabrikam.com
Now create a virtual directory named /Addy that maps to the physical folder C:\Code and which belongs to a site named New Site created previously by doing this:
appcmd add vdir /app.name:"New Site"/ /path:/addy /physicalPath:C:\code
VDIR object "New Site/addy" added
To create a new application (and also a new virtual directory) named Userinfo beneath the root of the site named New Site, and to map this application (and virtual directory) to the physical directory C:\Apps, do this:
appcmd add app /site.name:"New Site" /path:/userinfo /physicalPath:C\apps
You can also use Appcmd.exe to create and configure application pools and perform many other tasks. Appcmd.exe can be used to configure settings at the server, site, application, or virtual directory level. Appcmd.exe is found in the %WinDir%\System32\Inetsrv, which is not in the system path. This means to use this command, you need to either change your current directory to %WinDir%\System32\Inetsrv or add the \Inetsrv directory to your system path by typing setx path "%path%;C:\Windows\System32\inetsrv" /m and rebooting your computer.
For more information on how to do all these things, see my book Windows Server 2008 Server Core Administrator's Pocket Consultant from Microsoft Press.