【问题标题】:How to bulk update IIS configuration without consuming all available memory?如何在不消耗所有可用内存的情况下批量更新 IIS 配置?
【发布时间】:2014-12-09 18:53:34
【问题描述】:

set-webconfigurationproperty 的每次调用都会泄漏大量内存,并最终使当前进程达到MaxMemoryPerShellMb 的winrs 限制。

一个调用set-webconfigurationproperty的例子+循环检查当前进程工作集。 所有执行都在使用new-pssession 启动并使用invoke-command 执行的远程会话中进行。

[remoteserver]: PS C:\scripts> winrm get winrm/config/winrs
Winrs
    AllowRemoteShellAccess = true
    IdleTimeout = 7200000
    MaxConcurrentUsers = 10
    MaxShellRunTime = 2147483647
    MaxProcessesPerShell = 25
    MaxMemoryPerShellMB = 1024
    MaxShellsPerUser = 30

[remoteserver]: PS C:\scripts> 
1..100 | %{
    Set-WebConfigurationProperty -PSPath IIS:\ -filter "/system.webserver/rewrite/rewriteMaps/rewriteMap[@name='HostNameToServerMap']/add[@key='www.adomain.com']" -name "value" -value "iis04"
    new-object psobject -property @{ WorkingSet = "{0:f2} MB" -f ([System.Diagnostics.Process]::GetCurrentProcess().WorkingSet/1mb); Timestamp = get-date }
}


Timestamp                                                   WorkingSet
---------                                                   ----------
2014-10-14 22:10:54                                         964,45 MB
2014-10-14 22:10:56                                         981,76 MB
2014-10-14 22:10:57                                         999,95 MB
2014-10-14 22:10:58                                         1018,86 MB
Set-WebConfigurationProperty : Not enough storage is available to process this command. (Exception from HRESULT: 0x8007
0008)
At line:2 char:1
+ Set-WebConfigurationProperty -PSPath IIS:\ -filter "/system.webserver/rewrite/re ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-WebConfigurationProperty], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.SetConfigu
   rationPropertyCommand

2014-10-14 22:10:59                                         289,46 MB
2014-10-14 22:11:01                                         216,45 MB
2014-10-14 22:11:02                                         230,79 MB
2014-10-14 22:11:03                                         246,11 MB
2014-10-14 22:11:04                                         263,13 MB
2014-10-14 22:11:05                                         280,54 MB
2014-10-14 22:11:07                                         296,83 MB
2014-10-14 22:11:08                                         313,14 MB
2014-10-14 22:11:09                                         330,62 MB
2014-10-14 22:11:10                                         347,47 MB
2014-10-14 22:11:11                                         365,27 MB
....

set-webconfigurationproperty 消耗的内存没有被释放。 当进程达到 WinRS shell 的限制(在我的示例中为 1024 Mb)时会发生错误。

这个错误已经存在多年,现在我在 Server 2012 R2 上重现了它,因此 IIS 7、7.5、8 和 8.5 都会受到影响。

有没有比在 WinRM 会话中使用 WebAdministration PowerShell 模块的set-webconfigurationproperty 更好的方法来(批量)远程更新 IIS 配置?

【问题讨论】:

    标签: powershell iis powershell-remoting


    【解决方案1】:

    如果您使用的是 IIS 7 或 IIS 8,则可以使用名为 Microsoft.Web.Administration 的新托管 API。

    您可以在 Visual Studio 中通过 NuGet 获取此包。

    这是一个可以批量更新配置的 C# 示例代码。

    也许您可以将此控制台程序上传到您的服务器并运行它。

    更多信息,请阅读以下文章:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Web.Administration;
    
    namespace IISManager
    {
    
        class Program
        {
            static void Main(string[] args)
            {
                ServerManager serverManager = new ServerManager();
                foreach (Site site in serverManager.Sites)
                {
                    Console.WriteLine(site.Name);
                    Configuration config = site.GetWebConfiguration();
                    ConfigurationSection section = config.GetSection("system.webServer/samplesetion");
                    ConfigurationAttribute enabled = section.GetAttribute("enabled");
                    enabled.Value = true;
                    serverManager.CommitChanges();
                    Console.Read();
                }
    
            }
        }
    

    }

    使用 C# 管理 IIS——Microsoft.Web.Administration 命名空间

    http://msdn.microsoft.com/en-us/library/microsoft.web.administration(v=vs.90).aspx

    Programmatically create a web site in IIS using C# and set port number

    最好的问候。

    亚当斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      相关资源
      最近更新 更多