Monitor SharePoint Services (Such as Timer Service) – Send Alert Email When They Goes Down!

In continuation to my post: Monitor SharePoint App Pools using PowerShell Script, If certain Services on SharePoint server goes down, that could cause outages or service interruptions to your SharePoint environment! Although monitoring solutions such as SCOM can monitor services, they can’t start the service automatically – when stopped. So, let’s address this issue with the help of PowerShell! Here is my nifty PowerShell script to start required services when stopped and send alert emails.

Monitor SharePoint Services and Send Alert Email using powershell

This script not only scans services availability but also:

  • Detect services status if it is not in started state
  • Automatically starts the service if it’s in a stopped state
  • Sends alert Email to SharePoint Admin team (or whoever configured!)

PowerShell script to Monitor Services and Send Email notification:

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Configuration variables
$EmailFrom = "ServiceMonitor@crescent.com"
$EmailTo =  "SPAdmin@crescent.com" # Use commas for multiple addresses
$EmailSubject = "Service(s) went down in SharePoint Server"

#Services to Monitor
$ServicesToMonitor =  "IISADMIN", "SPTimerV4", "SPAdminV4", "SPTraceV4" , "SPUserCodeV4" , "SPWriterV4" , "OSearch14" , "W3SVC"

#Get Outgoing Email Server of the SharePoint Farm
$SMTP= (Get-SPWebApplication -IncludeCentralAdministration | Where { $_.IsAdministrationWebApplication } ) | %{$_.outboundmailserviceinstance.server.address}

#Check the status of each service
Foreach($ServiceName in $ServicesToMonitor)
{
 #Get the service
 $Service = Get-Service -Name $ServiceName
 
 #Check the Service status
 if ($Service.Status -ne "Running")
 {
  Write-Host $Service.DisplayName Found Not running!
  
  Try
  {
     #Set the Error Action
     $ErrorActionPreference = "Stop"
   #Try to start the service
   Start-Service $ServiceName
  }
  catch
  {
    Write-Host "Attempt to start service failed. Find the Error Message below:" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
  }
  finally
  {
    #Reset the Error Action to Default
    $ErrorActionPreference = "Continue"
  }

 #Send out the Alert E-mail 
 $EmailBody = "Hi SharePoint Team, `n `n The Service: $($Service.DisplayName) was found in stopped state!. `n`nWe tried Re-starting it... Current State of the Service: $($Service.Status). `n`nPlease take necessary actions if its not started! `n `nThanks, `nSharePoint Monitoring Script."
  Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTP -usessl
 }
}

How to Monitor services on each server of the SharePoint Farm?

Just add the parameter “-ComputerName $Server.Address” to “Get-Service” and “Start-Service” cmdlets.

So, the code goes like this:

#Get all SharePoint Servers
$ServersColl = Get-SPServer | where { $_.role -ne "Invalid"}

Foreach($Server in $ServersColl)
{
 #Wrap the above code
}

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! A 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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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