kwizcom banner advertisement

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:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$EmailFrom = "SharePointMonitor@crescent.com"
$EmailTo = @("salaudeen.rajack@crescent.com")
$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=""
    #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 error message:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$EmailFrom = "SharePointMonitor@crescent.com"
$EmailTo = @("salaudeen.rajack@crescent.com")
$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=""
    #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.
Monitor SharePoint Web Sites Availability and Send Alert Email using PowerShell Monitor SharePoint Web Sites Availability and Send Alert Email using PowerShell Reviewed by Salaudeen Rajack on March 26, 2016 Rating: 5

2 comments:

  1. 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

    ReplyDelete
    Replies
    1. make sure your code uses: -usessl parameter. Add your SharePoint server to the relay list in Exchange server.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.