Control Application Pool State with PowerShell 2.0

I came across a need last week to monitor for an event on a server when application pool stops. In this case, the application pool generates an error in the event log. The server monitoring tool we use has the ability to watch the windows event log.

I did a bit of searching and found some good posts. Shay Levi’s posts I found most helpful. But, I ended up taking a different approach.

I started experimenting with controlling the state of application pools using wmi and ADSI in PowerShell. I created three functions start-pool, recycle-pool, and stop-pool. I placed these into the WindowsPowerShell under the My Documents directory for the service account the server monitoring application runs as. I can then execute the PowerShell exe as an action in the following manner:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe start-pool "servername" "poolname"

I then set up my monitor to perform this action when the event log records an error for the application pool.

#requires -version 2
### Function name='Start-Pool'
function Start-Pool($Computer, $AppPool) {

### Function name='Stop-Pool'
function Stop-Pool($Computer, $AppPool) {

### Function name='Recycle-Pool'
function Recycle-Pool($Computer, $AppPool) {