0

Nearly finished with my project, however I can't seem to be able to get the final section working:

  1. Fetch all Application Pools on the server.
  2. Loop through all directories of the application pools until I find a match.
  3. Restart the matching application pool.

That's all I'm trying to do, however I can't get it working. The code I have managed to drag together so far, only returns a list of the application pool names:

    public static string[] FetchAppPools()
    {
        List<string> lvAppPools = new List<string>();

        DirectoryEntry lvWebService = new DirectoryEntry("IIS://localhost/W3SVC");
        IEnumerator ie = lvWebService.Children.GetEnumerator();
        DirectoryEntry lvServer = null;

        while(ie.MoveNext())
        {
            lvServer = (DirectoryEntry)ie.Current;

            if (lvServer.SchemaClassName == "IIsWebServer")
                lvAppPools.Add(lvServer.Properties["ServerComment"][0].ToString());
        }

        return lvAppPools.ToArray();
    }

How would I need to change the above code to also bring me back the directories associated with each of the application pools (C:\inetpub\Website1)?

Thank you.

  • http://stackoverflow.com/questions/249927/restarting-recycling-an-application-pool - This will help you – Developer Nov 02 '15 at 15:00
  • Thank you for the links, I have however changed my question (last paragraph) which should point you in the direction that I'm looking for. –  Nov 03 '15 at 08:36

1 Answers1

2

Can try this one

 var directoryEntry = new DirectoryEntry(APPLICATION_POOL_URL, USERNAME,   PASSWORD);

       // call to stop the Application Pool
     directoryEntry.Invoke("Stop", null);

      // call to start the Application Pool
          directoryEntry.Invoke("Start", null);

     // call to recycle the Application Pool
        directoryEntry.Invoke("Recycle", null);

Basically you have to call directoryEntry.Invoke("Stop/Start/Recycle", null);

Developer
  • 759
  • 2
  • 9
  • 24
  • Hi Partho, thank you for this, however I've just had a look on MSDN and I can't seem to find out whether "APPLICATION_POOL_URL" is the actual web address or the directory that it's bound to, would you be able to let me know which of those it is? Thank you. –  Nov 03 '15 at 08:15
  • The URL is like: "IIS://localhost/W3SVC/AppPools" – dethSwatch Apr 03 '18 at 14:24
  • I also found that I couldn't "Recycle" some pools unless I stop/start/recycle and I wonder if it might have been that the pools that were throwing a com error on .invoke weren't started... it also worked fine without using the 2 param version of .Invoke – dethSwatch Apr 03 '18 at 14:25