Monitor SharePoint Web Sites Availability and Send Alert Email using PowerShell

Requirement: Script to monitor SharePoint web sites and trigger an Alert Email when something goes wrong!

PowerShell script to Monitor SharePoint Sites:

Let’s have PowerShell to monitor our SharePoint web sites and trigger an Email when they go down!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$EmailFrom = "[email protected]"
$EmailTo = @("[email protected]")
$EmailSubject = "Alert: SharePoint site is Down!"
 
#Get Outgoing Email Server of the SharePoint Farm
$SMTPServer= (Get-SPWebApplication -IncludeCentralAdministration | Where { $_.IsAdministrationWebApplication } ) | % {$_.outboundmailserviceinstance.server.address}

#Make a web request to check site status
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true

#Iterate through each web application
Foreach($WebApp in (Get-SPWebApplication))
{
    $EmailBody=[string]::Empty
    #Get the timestamp
    $TimeStamp = Get-Date -f "yyyy-MM-dd HH:mm:ss"

    try
    {
        $Page = $WebClient.DownloadString($WebApp.Url)
    }
    catch [Exception]
    {
        $EmailBody += "The SharePoint site: $($WebApp.Url) is unavailable. Please take necessary action!<br><br>"
        $EmailBody += "<b>URL:</b> " + $WebApp.Url + "<br><br>"
        $EmailBody += "<b>Exception:</b> " + $_.Exception.message + "<br><br>"
        $EmailBody += "<b>Timestamp:</b> " + $TimeStamp

        #Send an Alert Email message
        Send-MailMessage -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -From $EmailFrom -BodyAsHtml -usessl
    }
} 

Here is the alert Email in action:

Monitor SharePoint Sites availability and Sent Alert Email when things go wrong using PowerShell

Schedule this script in Windows task scheduler to run for every 10 minutes.

Update: What if the site loaded with an error? E.g. “Sorry, something went wrong” error message.

sharepoint site monitor script

Here is the updated script to check if the site loaded with an error message:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$EmailFrom = "[email protected]"
$EmailTo = @("[email protected]")
$EmailSubject = "Alert: SharePoint site is Down!"
 
#Get Outgoing Email Server of the SharePoint Farm
$SMTPServer= (Get-SPWebApplication -IncludeCentralAdministration | Where { $_.IsAdministrationWebApplication } ) | % {$_.outboundmailserviceinstance.server.address}

#Make a web request to check site status
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true

#Iterate through each web application
Foreach($WebApp in (Get-SPWebApplication))
{
    $EmailBody=[string]::Empty
    #Get the timestamp
    $TimeStamp = Get-Date -f "yyyy-MM-dd HH:mm:ss"

    try
    {
        $Page = $WebClient.DownloadString($WebApp.Url)

        if($Page.Contains("Sorry, something went wrong"))
        {            
            $EmailBody += "The SharePoint site: $($WebApp.Url) encountered with an unexpected error. Please take necessary action!<br><br>"
            $EmailBody += "<b>URL:</b> " + $WebApp.Url + "<br><br>"
            $EmailBody += "<b>Timestamp:</b> " + $TimeStamp

            #Send an Alert Email message
            Send-MailMessage -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -From $EmailFrom -BodyAsHtml -usessl    
        }
    }
    catch [Exception]
    {
        $EmailBody += "The SharePoint site: $($WebApp.Url) is unavailable. Please take necessary action!<br><br>"
        $EmailBody += "<b>URL:</b> " + $WebApp.Url + "<br><br>"
        $EmailBody += "<b>Exception:</b> " + $_.Exception.message + "<br><br>"
        $EmailBody += "<b>Timestamp:</b> " + $TimeStamp

        #Send an Alert Email message
        Send-MailMessage -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -From $EmailFrom -BodyAsHtml -usessl
    }
}

You can add few more probable error messages such as: “cannot connect to the configuration database”, “Server error”, etc.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

4 thoughts on “Monitor SharePoint Web Sites Availability and Send Alert Email using PowerShell

  • The best it to add it into your warmup script

    Reply
  • Hello, excellent script, I would like to use it in my company, at this moment I am executing it but it jumps an error:

    Send-MailMessage: The remote certificate is not valid according to the validation procedure.
    In C: Users administrator Documents Missing SAG NoticeCitySite.ps1: 54 Character: 9
    + Send-MailMessage -To $ EmailTo -Subject $ EmailSubject -Body $ EmailBody -S …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         + CategoryInfo: InvalidOperation: (System.Net.Mail.SmtpClient: SmtpClient) [Send-MailMessage], AuthenticationException
         + FullyQualifiedErrorId: AuthenticationException, Microsoft.PowerShell.Commands.SendMailMessage

    Reply
    • make sure your code uses: -usessl parameter. Add your SharePoint server to the relay list in Exchange server.

      Reply

Leave a Reply