IIS7 provides quite a few ways to create websites, applications, and application pools. The simplest way of course is to use the new GUI Administration tool, where your new website is always a few mouse-clicks away.
But, what if you need to create 10, 100, or even 10,000 websites? Unless you own a farm of trained pigeons, using the GUI would certainly not be your first choice. Fortunately, IIS7 comes with quite a few alternatives for automated configuration management, including:
· AppCmd command line tool
· Scriptable COM-based API
· A WMI provider
· The .NET Microsoft.Web.Administration namespace.
All of these can be used to edit any IIS7 configuration, and most also provide simplified ways to create websites, applications, and application pools.
But, which one is the fastest way to get it done?
Both WMI, MWA, and scriptable COM have a pretty high upfront investment in developing the script/code to create the sites. AppCmd allows you to create a website in one line at the command line, and from this perspective it’s very hard to beat. This is the main reason why AppCmd is my weapon of choice in this situation.
More interestingly, it turns out that AppCmd can also provide the best performance for creating a large amount of websites on Windows Server 2008 when compared to the other options. This should come as a surprise, especially when you consider that with some extra work you can create a WMI/Ahadmin COM script or .NET program using MWA that create all of the sites in a single run, as opposed to starting AppCmd a 1000 times.
Indeed, you won’t get this performance if you write a batch file to run a 1000 of AppCmd Add Site commands, due to the high cost of starting a process and the expensive one-time initialization performed inside AppCmd.
To get this performance, you need to take advantage of, drum roll, command pipelining (You can learn more about it my previous post, Do complex IIS management tasks easily with AppCmd command piping).
Using AppCmd to bulk create IIS7 websites and application pools
To recap quickly, AppCmd supports piping the output of the LIST command as XML to another AppCmd command, which can take that output as its input. This allows multiple AppCmd commands to be chained together and essentially create a pipeline of related operations.
If we take a look at the output of %windir%system32inetsrvAppCmd List Sites /XML, we get the following:
<?xml version="1.0" encoding="UTF-8"?>
<SITE SITE.NAME="Default Web Site" SITE.ID="1" bindings="http/*:80:" state="Started" />
Now, what would happen if we created an XML file containing our site definitions in this format, and then piped them to the AppCmd Add Site command? Voila, you just discovered what I call AppCmd bulk loading.
With this in mind, our process becomes:
1) Write a batch file that generates the sites.xml (or apppools.xml) file containing site (or application pool) definitions. Here is an example:
FOR /L %%I IN (0,1,%1) DO echo ^<site name="Site%%I" bindings="http/*:80:site%%I" id="1%%I" physicalPath="%systemdrive%inetpubsite%%I" /^>
gensitexml.bat 1000 > sites.xml
2) Run the AppCmd Add Site command with the /IN switch with that file as input:
%windir%system32inetsrvAppCmd Add Sites /in < sites/xml
3) Watch our sites get created
In the case where you cannot use the AppCmd Add Site command’s short form for creating the site/root app/root vdir in a single command (by specifying the /physicalPath parameter), you can create the site, application, and virtual directory in 3 separate bulk operations. You may need to do this if you need to create non-root apps/virtual directories for the site, or set application or virtual directory settings like the app’s applicationPool. Just look at the output of AppCmd List Sites /XML, AppCmd List Apps /XML, and AppCmd List Vdirs /XML, create your bulk files as above, and feed them to AppCmd Add Site, AppCmd Add App, and AppCmd Add Vdir commands.
To learn more about creating websites, applications, virtual directories, and application pools with AppCmd, be sure to read my previous post: Creating IIS7 sites, applications, and virtual directories.
A final note: Windows Server contains a lot of performance improvements across the entire configuration stack, including many improvements in AppCmd that truly make bulk loading fly. You will not get this on your Windows Vista machine, unless you get the Vista SP1 Beta. However, there is one thing you can do to help AppCmd’s performance on Vista when bulk creating websites – be sure to stop WAS during the operation:
// create the sites
This speed up site creation multiple times, due to a last minute fix I snuck in that turns off website state retrieval during site creation. This won’t get you anywhere to server or Vista SP1 performance, but it will help out a bit.
Let me know if you have questions about bulk loading, or command piping, as those are both very powerful ways that AppCmd provides to help you manage IIS7.
There are plenty more things you can do with both, and I am looking forward to seeing them come up on the net as people discover the tool J