Freigeben über


Creating Application Pools in IIS

Internet Service Providers (ISPs), who provide Web hosting services to customers, need to configure their IIS servers frequently. Some ISPs use forms to programmatically sign up a new customer and add a new Web site or virtual directory for them. The new site or virtual directory can be put in a new application pool to isolate it from other applications on the server.

IIS 5.1 and earlier: The AppCreate3 method, the IIS WMI provider, and Application pools are not available, and therefore this topic does not apply.

Example

The following code example uses the JScript programming language to perform the following actions.

  • Create a new application pool called MyAppPool.

  • Create a new virtual directory called MyVDir in the default Web site.

  • Assign the new virtual directory to the new application pool.

For a code example that uses System.DirectoryServices that performs these actions, see Creating Application Pools Using System.DirectoryServices.

For this code to work, the IIS server must be running in worker process isolation mode because application pools do not exist in IIS 5.0 isolation mode.

CreateAppPool("Localhost", "MyAppPool");

CreateVDir("Localhost", "W3SVC/1/Root", "MyVDir", "D:\\Inetpub\\Wwwroot");

AssignVDirToAppPool("Localhost", "W3SVC/1/ROOT/MyVDir", "MyAppPool");
//CleanUp("Localhost", "W3SVC/1/ROOT/MyVDir", "MyAppPool");


function CreateAppPool(serverName, appPoolName)
{
  //  serverName is of the form "<servername>", for example "Localhost" 
  //  appPoolName is of the form "<name>", for example, "MyAppPool"
  WScript.Echo("\nCreating application pool named W3SVC/AppPools/" + appPoolName);

  try
  {
    // Connect to WMI, the IIS namespace, and the application pools node.
    var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
    var providerObj = locatorObj.ConnectServer(serverName, "root/MicrosoftIISv2");

    // Create the new application pool.
    var newpool = providerObj.Get("IIsApplicationPoolSetting").SpawnInstance_();
    newpool.Name = "W3SVC/AppPools/" + appPoolName;
    newpool.Put_();

    // Verify creation.
    newpool = providerObj.Get("IIsApplicationPool='W3SVC/AppPools/" + appPoolName + "'" );
    WScript.Echo(" Done creating " + newpool.Name);
  }
  catch (ex)
  {
    WScript.Echo("Failed in CreateAppPool with the following exception: \n" + ex);
  }
}

function CreateVDir(serverName, metabasePath, vDirName, physicalPath)
{
  //  serverName is of the form "<servername>", for example "Localhost" 
  //  metabasePath is of the form "<service>/<siteID>/Root[/<vdir>]", for example "W3SVC/1/Root" 
  //  vDirName is of the form "<name>", for example, "MyNewVDir"
  //  physicalPath is of the form "<drive>:\\<path>", for example, "C:\\Inetpub\\Wwwroot"
  WScript.Echo("\nCreating virtual directory " + metabasePath + "/" + vDirName);

  try
  {
    // Connect to WMI, the IIS namespace, and the application pools node.
    var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
    var providerObj = locatorObj.ConnectServer(serverName, "root/MicrosoftIISv2");
    var newVDir = providerObj.Get( "IIsWebVirtualDirSetting" ).SpawnInstance_();
    newVDir.Name = metabasePath + "/" + vDirName;
    newVDir.Path = physicalPath;
    newVDir.AppFriendlyName = vDirName;
    newVDir.AccessScript = true;
    newVDir.Put_();
  
    WScript.Echo(" Done.");
  }
  catch (ex)
  {
    WScript.Echo("Failed in CreateVDir with the following exception: \n" + ex);
  }
}

function AssignVDirToAppPool(serverName, metabasePath, appPoolName)
{
  //  serverName is of the form "<servername>", for example "Localhost" 
  //  metabasePath is of the form "W3SVC/<siteID>/Root[/<vDir>]", for example "W3SVC/1/ROOT/MyVDir" 
  //  appPoolName is of the form "<name>", for example, "MyAppPool"
  WScript.Echo("\nAssigning application " + metabasePath + " to the application pool named " + appPoolName);

  try
  {
    // Connect to WMI, the IIS namespace, and the application pools node.
    var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
    var providerObj = locatorObj.ConnectServer(serverName, "root/MicrosoftIISv2");

    var vDir = providerObj.Get( "IIsWebVirtualDir='" + metabasePath + "'" );

    if (-1 == vDir.AppRoot.indexOf(metabasePath))
    {
      WScript.Echo(" No application found on vdir so creating one.");
      // The following code creates an application on the vdir.
      var POOLPROC = 2;
      vDir.AppCreate3( POOLPROC, appPoolName, true );
    }
    else
    {
      WScript.Echo(" Application found on vdir so assigning to pool.");
      // The following code assigns a vdir to an application pool,
      // ONLY if there is an application created on the vdir already.
      var vDir = providerObj.Get( "IIsWebVirtualDirSetting='" + metabasePath + "'" );
      vDir.AppPoolID = appPoolName;
      vDir.Put_();
    }

    WScript.Echo(" Done.");
  }
  catch (ex)
  {
    WScript.Echo("Failed in AssignVDirToAppPool with the following exception: \n" + ex);
  }
}

function CleanUp(serverName, metabasePath, appPoolName)
{
  //  serverName is of the form "<servername>", for example "Localhost" 
  //  metabasePath is of the form "W3SVC/<siteID>/Root[/<vDir>]", for example "W3SVC/1/ROOT/MyVDir" 
  //  appPoolName is of the form "<name>", for example, "MyAppPool"
  WScript.Echo("\nDeleting vdir " + metabasePath + " and application pool " + appPoolName);

  try
  {
  // Connect to WMI, the IIS namespace, and the application pools node.
  var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
  var providerObj = locatorObj.ConnectServer(serverName, "root/MicrosoftIISv2");

    providerObj.Delete( "IIsWebVirtualDir='" + metabasePath + "'" );
    providerObj.Delete( "IIsApplicationPool='W3SVC/AppPools/" + appPoolName + "'" );

    WScript.Echo(" Done.");
  }
  catch (ex)
  {
    WScript.Echo("Failed in CleanUp with the following exception: \n" + ex);
  }
}