Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone/Duplicate/Copy an Existing Application Pool in IIS 7

Tags:

c#

iis

In PowerShell one can clone an existing IIS 7 application pool to a new application pool, retaining all the source pools settings in the new pool. Like this...

import-module webadministration
copy IIS:\AppPools\AppPoolTemplate IIS:\AppPools\NewAppPool -force

Now I would like to do the same thing in C# using the classes in the Microsoft.Web.Administration namespace. I have browsed the namespace and I cannot find a way to do this easily. There is the MemberwiseClone method that I could call to create a shallow copy of the existing app pool, but I don't know if that would duplicate all the original app pools properties.

Can anyone help?

like image 661
John Fitzpatrick Avatar asked Oct 07 '22 12:10

John Fitzpatrick


2 Answers

I have found only one way so far to copy an application pool in C#:

    private void creationizeAppPoolOldSchool(string strFullName)
    {
        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); 
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); 
        runspace.Open(); 
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
        scriptInvoker.Invoke("import-module webadministration");
        string str = "copy IIS:\\AppPools\\_JANGO_FETT IIS:\\AppPools\\" + strFullName + " –force";
        scriptInvoker.Invoke(str);
    }

Because my real need is to have a set of predefined settings on all new application pools, I actually abandoned copying an existing template pool and instead create an application pool with predefined settings using Microsoft.Web.Administration. Although that's not the original question, I share it anyway because someone browsing this post may also be interested in it:

    public static void CreateCoCPITAppPool(string strName)
    {
        using (ServerManager serverManager = new ServerManager())
        {
            ApplicationPool newPool = serverManager.ApplicationPools.Add(strName);
            newPool.ManagedRuntimeVersion = "v4.0";
            newPool.AutoStart = true;
            newPool.ProcessModel.UserName = "username";
            newPool.ProcessModel.Password = "password";
            newPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
            newPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero;
            newPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(10000); // .Zero;
            newPool.ProcessModel.ShutdownTimeLimit = TimeSpan.FromSeconds(3600);
            newPool.Failure.RapidFailProtection = false;
            serverManager.CommitChanges();
            IDictionary<string, string> attr = newPool.Recycling.RawAttributes;
            foreach (KeyValuePair<String, String> entry in attr)
            {
                // do something with entry.Value or entry.Key
                Console.WriteLine(entry.Key + " = " + entry.Value);
            }
            ConfigurationAttributeCollection coll = newPool.Recycling.Attributes;
            foreach (ConfigurationAttribute x in coll)
            {
                Console.WriteLine(x.Name + " = " + x.Value);
            }
        }
    }
like image 134
John Fitzpatrick Avatar answered Oct 10 '22 02:10

John Fitzpatrick


I'm not sure about a copy method, but you could access the current app pool's properties and then create a new app pool with the same properties:

// How to access a specific app pool
DirectoryEntry appPools = new DirectoryEntry("IIS://" + serverName + "/w3svc/apppools", adminUsername, adminPassword);
foreach (DirectoryEntry AppPool in appPools.Children)
{
    if (appPoolName.Equals(AppPool.Name, StringComparison.OrdinalIgnoreCase))
    {
        // access the properties of AppPool...
    }
}

Then you create a new pool in your code by calling the method listed below:

CreateAppPool("IIS://Localhost/W3SVC/AppPools", "MyAppPool");

App pool creation method from MSDN:

static void CreateAppPool(string metabasePath, string appPoolName)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
    //    for example "IIS://localhost/W3SVC/AppPools" 
    //  appPoolName is of the form "<name>", for example, "MyAppPool"
    Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);

    try
    {
        if (metabasePath.EndsWith("/W3SVC/AppPools"))
        {
            DirectoryEntry apppools = new DirectoryEntry(metabasePath);
            DirectoryEntry newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
            newpool.CommitChanges();
        }
        else
        {
            Console.WriteLine(" Failed in CreateAppPool; application pools can only be created in the */W3SVC/AppPools node.");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
    }
}
like image 33
DanM7 Avatar answered Oct 10 '22 03:10

DanM7