Table of Contents
One of the most boring things to do while configuring multiple web server nodes (cluster, server farm or other load balanced environments) is having to setup the entire website configuration tree in each IIS instance. Not to mention Application Pools, which are often a pain to configure properly.
Luckily enough, starting from IIS7+, there's a nice command-line utility called appcmd who can effectively export the entire IIS websites & app pools configuration in xml format and also import these same xml into another IIS instance. Let's see how we can do this.
Export the Application Pools
The first thing you have to do is to export/import the application pools, since they will most likely be used in your web sites and you won't be able to import those without having their relevant app pool into place. Here's the command-line:
1 |
%windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\apppools.xml |
This command will export all your application pools - including the default ones. You'll need to remove those, as they will most likely be in your target IIS instance with the same name and therefore they will raise a duplicate name error, blocking the whole import. In order to avoid that, open the newly created apppools.xml file and remove the default ones such as:
- DefaultAppPool
- Classic .NET AppPool
- .NET v2.0
- .NET v2.0 Classic
- .NET v4.5
- .NET v4.5 Classic
... And also each one that's already present in your target. If you forget one, don't worry - you'll figure it out when you'll try to run the import procedure, which won't work.
Import the Application Pools
Copy the apppools.xml file to your target webserver and run the following command:
1 |
%windir%\system32\inetsrv\appcmd add apppool /in < c:\apppools.xml |
Each and every Application Pool mentioned in the xml file will be created on your target IIS instance.
Export the Websites
Right after the App Pools you can copy your website configuration. Open up again a command-line console on your source webserver and type in the following command:
1 |
%windir%\system32\inetsrv\appcmd list site /config /xml > c:\websites.xml |
Again, you'll have to remove the default websites - you'll most likely have only one default website, which is Default Website - as well as any other website you don't want to copy and/or is already existing on the target IIS instance, otherwise the import command won't work.
Import the Websites
Just like you did with the App Pools file, copy the websites.xml file to your target webserver and run the following command:
1 |
%windir%\system32\inetsrv\appcmd add site /in < c:\websites.xml |
Export/Import a single App Pool or Website
These commands can also be used to export/import a single application pool or a specific website. You just have to add their identifying name to the command-line, such as:
Export a specific Application Pool
1 |
%windir%\system32\inetsrv\appcmd list apppool "CustomAppPool" /config /xml > c:\customapppool.xml |
Import a specific Application Pool
1 |
%windir%\system32\inetsrv\appcmd add apppool /in < c:\customapppool.xml |
Export a specific Website
1 |
%windir%\system32\inetsrv\appcmd list site "CustomWebsite" /config /xml > c:\customwebsite.xml |
Import a specific Website
1 |
%windir%\system32\inetsrv\appcmd add site /in < c:\customwebsite.xml |
That's all for now. Happy coding!
I got this error when I ran import app pools…
Let me know the error so maybe I can help.
I got the app pools to import successfully, but when I attempt to import the websites, I get:
C:>%windir%system32inetsrvappcmd add site /in < D:medtestmywebsite1.xml
ERROR ( hresult:8007000d, message:Command execution failed.
The data is invalid.
I changed the physical paths to match the directory structure, but still receive this error.
Please advise.
Me too.. Can’t find anything anywhere on this.. it’s the final step keeping me from running server migrations to new web farms!
For anyone else coming along, I had to fix the quotes in these snipits, they copy and pasted oddly. “CustomAppPool”
Very useful, thanks!
Excelente!
Gracias!!!
Hello Rayan,
I tried to execute the import script to import all app pools from cmd promt running in administrator mode but still im getting error as “Access Denied”. can you help me out.`
Hello Vivek,
I got this error some months ago: it was due to one of the %SystemRoot%\System32\inetsrv\config\ config files (those ending in .config) being read-only on the target machine.
It’s most likely a read-only issue.
launch cmd as administrator
Valid for
IIS 8.5 Windows Server 2012 R2 / Windows 8.1
toIIS 10.0 Windows Server 2016 / Windows 10
?When you do the import of a site or sites, you may get a “Failed to add duplicate collection element ” error (even after deleting the default site as warned about above).
The problem is that the export of the sites from the source server will have stored in the websites.xml the “id” of the site in IIS–as it was on THAT source server. But if you have defined any sites on the server you are importing TO, you may have sites that ALREADY use that ID. And THAT is the reason for the conflict (though the error does not make that clear). I experienced this and helped someone solve it.
The solution is to go to IIS, choose “sites” (which will show all your sites on the right), and click the “id” column to sort that list by id. Now look at the largest number there. Let’s say it’s 7. We can tell the websites.xml file to have all the sites start with 10 (it’s not important that there “be no gaps” in the number. Such gaps can happen when you merely delete a site in the IIS UI.)
So go into the websites.xml file, and you will see that for each site, there are TWO references to the site “ID” (in the first couple of XML lines per site). Change those two references to use that next number, like 10.
Then find the next site’s XML elements, and change its two site ID values to 11, and so on. Save the file, then try the import step again. That solved the problem for me.