在 IIS 7 中克隆/复制/复制现有应用程序池



在PowerShell中,可以将现有的IIS 7应用程序池克隆到新的应用程序池,同时保留新池中的所有源池设置。 喜欢这个。。。

import-module webadministration
copy IIS:AppPoolsAppPoolTemplate IIS:AppPoolsNewAppPool -force

现在我想使用 Microsoft.Web.Administration 命名空间中的类在 C# 中做同样的事情。 我已经浏览了命名空间,但找不到轻松执行此操作的方法。 我可以调用 MemberwiseClone 方法来创建现有应用程序池的浅表副本,但我不知道这是否会复制所有原始应用程序池属性。

谁能帮忙?

到目前为止,

我只找到了一种在 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);
            }
        }
    }

我不确定复制方法,但您可以访问当前应用程序池的属性,然后创建具有相同属性的新应用程序池:

// 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);
    }
}

最新更新