Tuesday, December 29, 2015

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, lets address this issue with the help of PowerShell! Here is my nifty PowerShell script to start required services when stopped and send alert emails .

This script not only scans services availability, but also:

  • detects services status if its not in started state
  • Automatically starts the service if its in 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!
     #Set the Error Action
     $ErrorActionPreference = "Stop"
   #Try to start the service
   Start-Service $ServiceName
    Write-Host "Attempt to start service failed. Find the Error Message below:" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
    #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 parameter "-ComputerName $Server.Address" to "Get-Service" and "Start-Service" cmdlets.
So, the code goes like:
#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! 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...