Monitor application pool IIS recycle if needed


In working with an issue where an application pool was taking an entire website down, it became apparent we needed a programmatic way to monitor the site itself and then take proactive action in an automated way to correct the problem.  In the case/environment this applied to in my case, a problematic application pool periodically takes down an IIS site.  At first iisresets were used to get things back up and running, however, it was found that the application pool could be recycled and the problem would clear as well.  The objective was clear – we needed to monitor application pool IIS recycle if needed.  How can we accomplish this?

Monitor application pool IIS recycle if needed

In any case, if a website is having issues, we should be able to monitor a keyword or content on the site and then perform an action.  In taking a look around the Net concerning the topic of web monitoring, I stumbled on a great post here which shows using powershell and the WebClient .Net to evaluate returned HTML content.  So essentially, you can check for the existence or nonexistence of a keyword and then perform an action based on that result.

This script can be customized as far as you want to take it and OttoHelweg2 shows how to monitor multiple sites with the script, log and alert.  The single site version of the script works for me with just a tad bit of customization as I wanted to perform an action on the problematic application pool (specifically recycle it) if I see problems with the returned web content.  Easily enough, there is a great powershell commandlet which lets you do just that – Restart-WebAppPool.  Using this commandlet we can just feed in the name of the AppPool and have it recycled if you have issues with an IIS site.  So changing the else statement of the script I hyperlinked above:

The nice thing is that if you need something more profound than a simple restart of an application pool, you can do it by substituting any command you would like here, i.e. iisreset, etc.

As I have logging setup above in the else clause for a failure, we note when failures occur and log these accordingly.  The log that is created looks like the folloing which I simulated an error by taking the site down.

Scheduled Task

As mentioned in linked post above, the script is most effective in a scheduled task which can easily be setup with passing the powershell script parameters inside the scheduled task.  Read how to do this with the recently published post:  Powershell script signing the easy way.

You can set the schedule task to run every minute at the lowest setting, but if you utilize multiple tasks, you can overlap these so that it runs every few seconds.


Powershell is a powerful tool even in monitoring website health and can even be used to proactively repair IIS if services need restarted, etc.  Hopefully, with the modified script above adding to the original script created you can Monitor application pool IIS recycle if needed.