Sunday, February 1, 2015

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 SharePoint IIS web application's application pool status, it makes bit more noisy with alerts and doesn't start the application pool automatically - when stopped. So, lets address this issue with the help of PowerShell! Here is my nifty PowerShell script to monitor application pools on all SharePoint web front end servers.

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:
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: $($ in stopped state at the server : $($Server) on $($now)" >> $LogFile
        "Trying to Start the application Pool...">> $LogFile
        #Try Starting the application Pool
        Start-Sleep -s 10
        "Application Pool's current Status: $($AppPool.State)" >> $LogFile

        #Send Alert-Mail message
        $emailFrom = ""
        # Use commas for multiple addresses
        $emailTo = ""
        $subject = "Application Pool: $($AppPool.Name) in stopped state in Server: $($Server) at $($now)"
        $body = "Hi SharePoint Team, `n `n The Application Pool $($ 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 = "" #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 IIS AppPool monitoring script:

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 my another post on Scheduling PowerShell scripts using Windows Task scheduler: Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...