Saturday, March 26, 2016

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.

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 might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


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