Application Pool Monitoring for SharePoint using PowerShell Script

If SharePoint Application Pools are stopped that would cause “Service Unavailable” outages to your SharePoint environment! While SCOM can monitor the SharePoint IIS web application’s application pool status, it makes it a bit noisier with alerts and doesn’t start the application pool automatically – when stopped. So, let’s address this issue with the help of PowerShell!

This script not only scans IIS Application Pool status on SharePoint Web-Front end servers but also:

  • Logs application Pool status if its not in Started state
  • Automatically starts AppPool if its in stopped state
  • Sends out an Alert-Email to SharePoint Admin team (or whoever configured!)

PowerShell script to Monitor IIS Application Pool statues:

Here is my nifty PowerShell script to monitor application pools on all SharePoint web front-end servers.

Import-Module WebAdministration

#Array to hold Server names - Change it to YOUR SharePoint front end servers
$WFEServers =("HS-WFE01", "HS-WFE02", "HS-WFE03")

#Log file location
$LogFile = "D:\Scripts\AppPool-Log.txt"

#Loop through each server and Check Application Pool status
foreach ($Server in $WFEServers)
{
    $ServerMgr = [Microsoft.Web.Administration.ServerManager]::OpenRemote($Server)

    #Get all Application Pools which are not in Started State
    $AppPoolColl = $ServerMgr.ApplicationPools | Where-Object {$_.State -ne "Started"}

    foreach($AppPool in $AppPoolColl)
    {
        #Get the time to Log
        $now = Get-Date -f "yyyy-MM-dd HH:mm:ss"

        #Log to file
        "`n Found Application Pool: $($AppPool.name) in stopped state at the server : $($Server) on $($now)" >> $LogFile
        "Trying to Start the application Pool...">> $LogFile
        
        #Try Starting the application Pool
        $AppPool.Start()
        Start-Sleep -s 10
        "Application Pool's current Status: $($AppPool.State)" >> $LogFile

        #Send Alert-Mail message
        $emailFrom = "AppPoolMonitor@crescent.com"
        # Use commas for multiple addresses
        $emailTo = "SPAdmins@crescent.com"
        $subject = "Application Pool: $($AppPool.Name) in stopped state in Server: $($Server) at $($now)"
        $body = "Hi SharePoint Team, `n `n The Application Pool $($AppPool.name) was in stopped state in server: $($server). `n`n We tried Re-starting it... Current State of the Application Pool: $($AppPool.State). `n`n Please take necessary actions if its not started !. `n `nThanks, `nSharePoint AppPool Monitoring Script."
        $smtpServer = "smtp.crescent.com" #IP or HOST Name of SMTP Server
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body) 
    } 
}

Here is the sample alert from the IIS AppPool monitoring script:

Application Pool Monitoring for SharePoint using PowerShell

Schedule this PowerShell script in Windows Task scheduler in any Application server (or any other server will do!) to periodically scan App Pool status, Say once per 5 Min! run interval can be adjusted based on your application priority.

Here is another post on Scheduling PowerShell scripts using Windows Task scheduler: Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

2 thoughts on “Application Pool Monitoring for SharePoint using PowerShell Script

  • Does not run on Server 2012 R2 Datacenter With IIS 8.0

    The specified module ‘WebAdministration’ was not loaded because no valid module file was found in any module directory.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *