// This code shows how to programmatically change the recycling schedule of an application pool using C# and ADSI

// ------------------------
// This requires the following using statements :
using System.Collections.Generic;
using System.DirectoryServices;
// ------------------------

// First let's connect to our Application pool. where ApplicationPool is the name of your application pool.
DirectoryEntry ApplicationPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools/ApplicationPool");

// Get the existing values from our Application Pool
PropertyValueCollection ExistingProperties = ApplicationPool.Properties["PeriodicRestartSchedule"];

// Add the desired value to the collection
ExistingProperties.Add("10:00");

// This can be multiple new values
//ExistingProperties.Add("15:00");

// Create an object to hold the new values
Object[] newValueList = new Object[ExistingProperties.Count];
ExistingProperties.CopyTo(newValueList, 0);

// Write the new values to the metabase
ApplicationPool.Properties["PeriodicRestartSchedule"].Value = newValueList;

// Another way to construct the Object value is by
//ApplicationPool.Properties["PeriodicRestartSchedule"].Value = new Object[] { "04:04", "02:34" };

// Commit our changes
ApplicationPool.CommitChanges();
Tagged with:
 

3 Responses to Programmatically change Application Pool recycling schedule

  1. johannes says:

    You are quite correct,  The object value can be constructed that way as well.  This is what I love about programming,  there is no one way to do things.

    I’m adding this to the code sample.

  2. Anonymous says:

    Hi,

    I have succeeded in creating an app pool on XP64 and Win2003 but neither want to set any of my property values? I also want to change the horrible recycle every 1740 mins setting etc.

    THe following runs but doesnt seem to change anything?

     

     

    // Get the existing values from our Application Pool

     

    lExistingProperties.Add(lExistingProperties.CopyTo(lnewValueList, 0);

     

    PropertyValueCollection lExistingProperties = newPool.Properties["PeriodicRestartSchedule"];"4:01");Object[] lnewValueList = new Object[lExistingProperties.Count];// Write the new values to the metabase

    newPool.Properties["PeriodicRestartSchedule"].Value = lnewValueList;

     

    newPool.CommitChanges();

    Any theory’s why the property settings are not changing?

    Brett

     

     

    newPool.Properties["PeriodicRestartMemory"].Value = "0";

     

    newPool.Properties["PeriodicRestartPrivateMemory"].Value = "0";

    newPool.Properties["PeriodicRestartTime"].Value = "0";//

     

  3. Anonymous says:

    Hi,

    just for anyone who is wondering.

    if you iterate the properties collection before commiting you loose all changes you made!

    I had a foreach to print out the new values before I commited.

    Strange but true

    Brett

     

    BTW: simpler code is (you can pass vars to this if you wish, or create an object array):

     

    "PeriodicRestartSchedule"].Value = new Object[]{"04:04", "02:34"};

    lJustCreatedPool.Properties[