Create email notification for SharePoint health analyzer result using Windows PowerShell

Steps:

1. Create a file with extension .ps1, e.g. SPErrNotification.ps1

2. Edit the file and paste the following script

 

if ($PSVersionTable) {$Host.Runspace.ThreadOptions = ‘ReuseThread’}
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$serverName = $args[0]

#Get Central Admin Web Application and Web objects
$caWebApp = (Get-SPWebApplication -IncludeCentralAdministration) | ? { $_.IsAdministrationWebApplication -eq $true }
$caWeb = Get-SPWeb -Identity $caWebApp.Url

#Set up from, to and server addresses
$toAddress = "admin1@company.com"
$ccAddress = "admin2@company.com"
$fromAddress = $caWebApp.OutboundMailReplyToAddress
$serverAddress = $caWebApp.OutboundMailServiceInstance.Server.Address

#Get Health Analyzer list on Central Admin site
$healthList = $caWeb.GetList("\Lists\HealthReports")
$displayFormUrl = $caWeb.Url + ($healthList.Forms | where { $_.Type -eq "PAGE_DISPLAYFORM" }).ServerRelativeUrl
$queryString = "<Where><Neq><FieldRef Name=’HealthReportSeverity’ /><Value Type=’Text’>4 – Success</Value></Neq></Where>"
$query = New-Object Microsoft.SharePoint.SPQuery
$query.Query = $queryString
$items = $healthList.GetItems($query)
#Set up e-mail message subject and HTML body
$DateToday = Get-Date
$msgTitle = $serverName + " – Health Analyzer results for farm " + $caWebApp.Farm.Name + " – " + $DateToday
$msgSubject = $serverName + " – Health Analyzer results – " + $DateToday
#HTML head
$head = "<style type=`"text/css`">.tableStyle { border: 1px solid #000000; font-size:11pt; }</style>"
$head = $head + "<Title>$msgTitle</Title>"
#Create HTML body by walking through each item and adding it to a table
$body = "<H3>$msgTitle</H3><table cellspacing=`"0`" class=`"tableStyle`" style=`"width: 100%`">"
foreach ($item in $items)
{
    $itemUrl = $displayFormUrl + "?id=" + $item.ID
    [array]$itemValues = @($item["Severity"], $item["Category"], $item["Explanation"], $item["Modified"])
    $body = $body + "<tr>"
    $body = $body + "<td class=`"tableStyle`"><a href=`"" + $itemUrl + "`">" + $item.Title + "</a></td>"
    $itemValues | ForEach-Object {
        $body = $body + "<td class=`"tableStyle`">$_</td>"
    }
    $body = $body + "</tr>"
}
$body = $body + "</table>"
#Create message body using the ConvertTo-Html PowerShell cmdlet
$msgBody = ConvertTo-Html -Head $head -Body $body
#Create e-mail message object using System.Net.Mail class
$msg = New-Object System.Net.Mail.MailMessage($fromAddress, $toAddress, $msgSubject, $msgBody)
$msg.IsBodyHtml = $true
$msg.cc.Add($ccAddress)
#Send message
if ($items.Count -gt 0)
{
    $smtpClient = New-Object System.Net.Mail.SmtpClient($serverAddress)
    $smtpClient.Send($msg)
}
$caWeb.Dispose()

3. Schedule to run the script using Windows Task Scheduler

powershell.exe –noexit  [FilePath]\SPErrNotification.ps1 [SERVERNAME]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s