Using Configuration Editor: Generate Scripts

By Crystal Hoyer

March 20, 2008

Walkthrough Goal

This walkthrough is aimed to demonstrate how to use Configuration Editor's Generate Script functionality through the example of Application Pool generation.

Prerequisites

This walkthrough requires the following prerequisites:

Part 1 – Create an application pool using the configuration editor

  1. Launch the Configuration Editor in the IIS Manager by double clicking the “Configuration Editor” feature from a server connection.
  2. Under the Section: dropdown menu click on system.applicationHost -> applicationPools
  3. In the property grid displayed below, click on the ellipse “…” on the first row, which corresponds to the (Collection) item.
  4. In the Collection Editor, you will see a list of the application pools that already exists.
  5. On the action pane, click “Add”
  6. Fill in the property grid displayed in the Properties section.
    1. Notice the “name” property has a key in symbol. This indicates a required field. Enter “applicationPool1” as your name to create a new application pool.
    2. Select processModel to expand.
    3. Set userName to “PoolId1”
    4. Set password to “PoolId1Pwd”
    5. Change identityType to “SpecificUser”
    6. You can optionally change other settings here
  7. [optional] To commit the changes to the configuration system, click Apply on the Actions pane. However, for the purpose of our walkthrough, please do not do so.

Part 2 – Generate script

After closing the properties dialog, click on the Generate Script link on the Actions pane. The 3 tabs of the Script Dialog displays 3 types of scripts to accomplish the actions we did on this section.

Managed Code: C# code snippet to create ‘applicationPool1’

using System;
using Microsoft.Web.Administration;

internal static class Sample {

private static void Main() {

ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();

ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");

ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();

ConfigurationElement addElement = applicationPoolsCollection.CreateElement("add");
addElement["name"] = @"applicationPool1";

ConfigurationElement processModelElement = addElement.ChildElements["processModel"];
processModelElement["identityType"] = @"SpecificUser";
processModelElement["userName"] = @"PoolId1";
processModelElement["password"] = @"PoolId1Pwd";
applicationPoolsCollection.Add(addElement);

serverManager.CommitChanges();
}
}

Scripting: Jscript code to create ‘applicationPool1’

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST");

var applicationPoolsCollection = applicationPoolsSection.Collection;

var addElement = applicationPoolsCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "applicationPool1";
var processModelElement = addElement.ChildElements.Item("processModel");
processModelElement.Properties.Item("identityType").Value = "SpecificUser";
processModelElement.Properties.Item("userName").Value = "PoolId1";
processModelElement.Properties.Item("password").Value = "PoolId1Pwd";
applicationPoolsCollection.AddElement(addElement);


adminManager.CommitChanges();

Command Line: Appcmd commands to create ‘applicationPool1’ and specify an identiy.

appcmd.exe set config -section:system.applicationHost/applicationPools /+".[name='applicationPool1']" /commit:apphost
appcmd.exe set config -section:system.applicationHost/applicationPools /.[name='applicationPool1'].processModel.identityType:"SpecificUser" /.[name='applicationPool1'].processModel.userName:"PoolId1" /.[name='applicationPool1'].processModel.password:"PoolId1Pwd" /commit:apphost

Part 3 – Create a C# program that creates 10 application pools

Based on the code generated by the Configuration Editor, we will refactor the code to create one application pool to the method “CreateApplicationPool”. The final code looks like this:

using System;
using Microsoft.Web.Administration;
 
internal static class Sample {
  private static void Main() {
  ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
 
ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
 
ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();
 
for (int i = 0; i < 10; i++) {
CreateApplicationPool(applicationPoolsCollection,
"AppPool" + i.ToString(),
  "AppPoolId" + i.ToString(),
"AppPoolPwd" + i.ToString());
}
serverManager.CommitChanges();
}
 
private static void CreateApplicationPool(ConfigurationSection applicationPoolsSection,
string appPoolName,
string appPoolUserName,
string appPoolPassword) 
{
  ConfigurationElement addElement = applicationPoolsCollection.CreateElement("add");
addElement["name"] = appPoolName;
 
ConfigurationElement processModelElement = addElement.ChildElements["processModel"];
processModelElement["identityType"] = @"SpecificUser";
processModelElement["userName"] = appPoolUserName;
processModelElement["password"] = appPoolPassword;
applicationPoolsCollection.Add(addElement);
}
}

Summary

You have now created code to create 10 application pools with aid from the Configuration Editor.



Discuss in IIS Forums