Search code examples
powershelliispowershell-remoting

How to bulk update IIS configuration without consuming all available memory?


Each call to set-webconfigurationproperty will leak a decent amount of memory and will eventually make the current process to hit the MaxMemoryPerShellMb limit of winrs.

An example of calling set-webconfigurationproperty + checking the current process working set in a loop. All execution is made inside a remote session initiated with new-pssession and carried out using 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
....

The memory consumed by set-webconfigurationproperty is not being released. The error occurs when the process hits the limit for a WinRS shell (1024 Mb in my example).

This bug have been around for years and now I have reproduced it on Server 2012 R2, thus IIS 7, 7.5, 8 and 8.5 are all affected.

Is there a better way to (bulk) update the IIS configuration remotely than using set-webconfigurationproperty of the WebAdministration PowerShell module in a WinRM session?


Solution

  • If you're using IIS 7 or IIS 8, you can use the new managed API called Microsoft.Web.Administration.

    You can get this package by NuGet in Visual Studio.

    Here is a sample code in C# which can bulk update the configuration.

    May be you can uplode this Console program to your server and run it.

    For more information, please read the following artical:

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

    }

    Using C# to Manage IIS – Microsoft.Web.Administration Namespace

    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

    Best regards.

    Adamus