Powershell to check if an application pool has stopped

I had an oddball situation where the app pool of a given site would keep stopping. No one visits this site with a browser, it’s used just for search crawling. So when it goes down, we never know.

In theory SCOM can tell us this, but SCOM produces so much noise that it’s easy to miss things.

import-module webadministration

	function main{
	#get-command -module webadministration will show all the IIS stuff
	$appPoolName = "SharePoint - www.mysite.com443"
	
	$dt = get-date
	$ComputerName = $env:computername
	If((get-WebAppPoolState -name $appPoolName).Value -eq "Stopped")
	{
		write-host "Failure detected, attempting to start it"
		start-webAppPool -name $appPoolName
		start-sleep -s 60
		
		If((get-WebAppPoolState -name $appPoolName).Value -eq "Stopped")
		{
			write-host "Tried to restart, but it didn't work!"
			sendmail "AppPoolRestart Failed" "App Pool $appPoolName restart on $ComputerName failed - this will effect search `n $dt"
			#log to event log
		}
		else
		{
			write-host "Looks like the app pool restarted ok"
			$subjectString = "AppPool Restart was needed"
			$body = "A routine check of the App Pool $appPoolName on $ComputerName found that it was not running, it has been started. `n $dt"
			sendmail $subjectString $body
			#log to event log?
		}
	}
	else
	 {
	 write-host "app pool $appPoolName is running"
	 }
 } #end main function
 
 function sendmail($subject, $body)
 {
    
    write-host "in Sendmail with subject: $subject, and body: $body"
	
	$EmailFrom = "WEBMONITOR@mydomain.com"
	$EmailTo = "jack@mydomain.com"
	$EmailBody = $body
	$EmailSubject = $subject
	$SMTPServer = "DNS.name.of.your.internal.smtp.server.com"
 
	Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $subject -body $EmailBody -SmtpServer $SMTPServer
	"Emailed $subject on $dt to $EmailTo" | out-file -filepath "CheckSearchAppPools.log" -append 
 }
 
 #call main function
 main

11 thoughts on “Powershell to check if an application pool has stopped

  1. Hi was searching around for a script like this and it worked perfectly!!!

    Would there be a chance to add additional IIS App Pools to be monitored?

    Sorry I’m new to this powershell scripting

    1. Yep that would be reasonably easy.
      If you know which App Pools you want, you could use an array
      $appPools = @(“pool1”, “pool2”, “etc”)

      Then use a foreach ($appPoolName in $appPools)
      {
      ..existing code
      }

      If you needed a more automated way of doing it, there are some commands that you can use to get a list of all app pools.

      1. Great post:
        Having some issues with my script, seems everything works except the first app pool when you DIR the IIS:/AppPools selection.

        Example, when I DIR AppPools, the first returned value is ASP.NET v4.0, second one is ASP.NET v4.0 Classic. First one starts but doesnt send the SMTP email, second one starts and does send the SMTP message.

        Any help is greatly appreciated!

        Add-PSSnapin webadministration

        function main{

        $dt = get-date
        $ComputerName = $env:computername

        cd IIS:/AppPools

        $ApplicationPools = dir

        foreach ($item in $ApplicationPools)

        {
        $ApplicationPoolName = $item.Name
        $ApplicationPoolStatus = Get-WebAppPoolState $ApplicationPoolName
        if($ApplicationPoolStatus.Value -eq “Stopped”)

        {

        Start-WebAppPool -Name $ApplicationPoolName
        start-sleep -s 10

        if($ApplicationPoolStatus.Value -eq “Stopped”)
        {

        sendmail “AppPoolRestart Failed” “App Pool $appPoolName restart on $ComputerName failed `n $dt”

        }
        else
        {

        $subjectString = “AppPool Restart was needed on $ComputerName”
        $body = “A routine check of the App Pool – $ApplicationPoolName on $ComputerName has reported that it was not running, it has been started. `n $dt”
        sendmail $subjectString $body

        }
        } #end main function

        function sendmail($subject, $body)

        {

        $EmailFrom = “MSNotifications@xxx.com”
        $EmailTo = “xxx@xxx.com”
        $EmailBody = $body
        $EmailSubject = $subject
        $SMTPServer = “xxx.com”

        Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $subject -body $EmailBody -SmtpServer $SMTPServer
        “Emailed $subject on $dt to $EmailTo” | out-file -filepath “C:\ScheduledTasks\CheckAppPools.log” -append
        }
        }
        }

        main

        1. HI JACK!

          I tried to change your code for multiple email id’s.

          [string[]]$EmailTo = “email1, email2, email3”

          The Email only goes to email3 not to email1 or email2?

          Only email3 the last email in the string array

          1. I remember messing with this and found it wasn’t as easy as it could be.
            The help file (get-help Send-MailMessage -Detailed) shows this format (in example 2):
            ARG- It looks like wordpress is messing with the format a bit – it might have in your original post as well….

            Try get-help Send-MailMessage -detailed and have a look at example 2.

          2. Hi Shujaath – thanks for letting me know!

            I assume you were talking about the extra .com on line 42 $EmailTo = …
            I’ve removed the extra .com

            Thanks again for your comments!

  2. Thanks for the post. I am new with PS scripting and I was wondering how this works: is the script to be issued by a scheduled job for example or it detects when the app poll is going down?
    thank you very much!

Leave a Reply to JackCancel reply