SharePoint PowerShell Script to Monitor SharePoint Availability

A little while ago on Reddit I read a post on the SharePoint Subreddit about monitoring a SharePoint 2010 environment. The #1 comment was from a user mentioning that since his fellow administrators hadn’t set up System Center Operations, he’d created a bunch of PowerShell scripts to monitor various aspects of SharePoint.

Since we also don’t use SCOM, I decided I’d write my own small script to monitor SharePoint to make sure it’s available from a basic level: just by checking to see if it’s possible to download the various site collection’s home page via HTTP, or if there are text that result from typical errors (“Troubleshoot issues with Microsoft SharePoint Foundation” for example). If the HTTP download fails, or common error text is found, shoot off an email to the admin(s). I’ve configured this script to run every 10 minutes on our farm; you may want to adjust this based off your SLA’s, governance, and experience.

Now, keep in mind that this is no-frills monitoring, canary in the coal mine type stuff. It’s not going to tell you what is wrong, but it will tell you that your clients are most probably having connection issues with SharePoint, and that aghem, you may want to look into that before the inevitable flood of “hey is SP down?” tickets and emails come your way.

#*=============================================
#* Script Name: Check SharePoint Status
#* Author: Nik Craik
#*=============================================
#* Purpose: Monitor SharePoint 2010 for errors
# and availability. If an error or problem
# is detected on a site, send an email to
# administrators.
#*=============================================
 
#Send an Email if an Error is encountered
function sendMail($site){
#SMTP server name
$smtpServer = "mail.yourserver.com"
 
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
 
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
 
#Email structure
$msg.From = "sharepoint@yourserver.com"
$msg.ReplyTo = "sharepoint@yourserver.com"
$msg.To.Add("sharepointadmin@yourserver.com")
$msg.To.Add("sharepointadmin2@yourserver.com")
$msg.Priority = [System.Net.Mail.MailPriority]::High
$msg.subject = "ALERT: "+$site+" IS UNAVAILABLE"
$msg.body = "The SharePoint site collection $site is unavailable. Please have an administrator review the problem."
 
#Sending email
$smtp.Send($msg)
}
 
#Try to download the home page of a given site collection, if fails, send email
function checkIfSiteUp($url){
#Create a new web client object with the current credentials
$webclient = new-object System.Net.WebClient
$webClient.UseDefaultCredentials = $true
 
#Attempt to download site's home page, if fails, send email
try {
$page = $webclient.DownloadString($url)
$errorPage = $page.Contains("Troubleshoot issues with Microsoft SharePoint Foundation.")
$serverError = $page.Contains("Server Error")
$configDBOffline = $page.Contains("Cannot connect to the configuration database.")
if ($errorPage -or $serverError -or $configDBOffline) {
sendMail($url)
}
}
catch {
sendMail($url)
}
}
 
#Array of SharePoint Sites
$shptWebs = @("http://intranet",
"http://othersite",
"http://onemoresite")
 
#Execute for all SharePoint Sites
foreach ($web in $shptWebs)
{
checkIfSiteUp($web)
}