Clone / Duplicate / Copy an existing application pool in IIS 7

In PowerShell, you can clone an existing IIS 7 application pool into a new application pool, storing all source pool 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 in C # using classes in the Microsoft.Web.Administration namespace. I looked through the namespace and I cannot find a way to do this easily. There is a MemberwiseClone method that I could call to create a shallow copy of an existing application pool, but I don't know if this will duplicate all the original properties of the application pools.

Can anyone help?

+5
source share
2 answers

I found only one way to copy the 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);
    }

, , Microsoft.Web.Administration. , , -, , :

    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);
            }
        }
    }
+3

, , :

// 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...
    }
}

, , :

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

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);
    }
}
+1

All Articles