4

#PSTip Getting SharePoint 2010 Health Analyzer alerts report by email

SharePoint 2010 administrators are probably familiar with the following screenshot–you open the Central Administration page and you’re presented with a red Health Analyzer alert.

sphealth

Clicking the ‘View these issues’ link takes you to a page that lists all items that needs an attention.

sphealth1

Checking the health alerts page each day can be a daunting task and you might also forget to do so. To avoid that, and to enable multiple team members to be aware of the alerts, you can  send the alerts by email. The Health list view (All Reports) is configured to list all items with a Severity not equal to Success (4).

sphealth2

Using the following code, you can read all items, and generate an email that you can send to your team members. Put it in a daily scheduled task on the SharePoint server and you’re good to go.

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

# get the health reports list
$ReportsList = [Microsoft.SharePoint.Administration.Health.SPHealthReportsList]::Local
$FormUrl = '{0}{1}?id=' -f $ReportsList.ParentWeb.Url, $ReportsList.Forms.List.DefaultDisplayFormUrl

$body = $ReportsList.Items | Where-Object {$_['Severity'] -ne '4 - Success'} | ForEach-Object {

    New-Object PSObject -Property @{
        Url = "<a href='$FormUrl$($_.ID)'>$($_['Title'])</a>"
        Severity = $_['Severity']
        Category = $_['Category']
        Explanation = $_['Explanation']
        Modified = $_['Modified']
        FailingServers = $_['Failing Servers']
        FailingServices = $_['Failing Services']
        Remedy = $_['Remedy']
    }

} | ConvertTo-Html | Out-String

# creating clickable HTML links
$body = $body -replace '&lt;','<' -replace '&gt;','>' -replace '&quot;','"'

$params = @{
	To = 'you@domain.com','manager@domain.com'
	From = 'SPHealth@domain.com'
	Subject = 'Daily Health Analyzer report'
	SmtpServer = 'smtp1'
	Body = $body
	BodyAsHtml = $true
}

Send-MailMessage @params

This is how it looks in Outlook (partial view).

sphealth3

Filed in: Columns, Tips and Tricks Tags: , ,

4 Responses to "#PSTip Getting SharePoint 2010 Health Analyzer alerts report by email"

  1. 大海 方 says:

    Thanks Shay! This post saved me a lot of time!

    To avoid SMTP service, I utilized the SharePoint Utility tool to send out the email, which also works well.

    $headers = New-Object System.Collections.Specialized.StringDictionary
    $headers.Add(“to”, “spadmin@domain.com”)
    #$headers.Add(“cc”, “test2@abc.com”)
    #$headers.Add(“bcc”, “test3@abc.com”)
    $headers.Add(“from”, “SPHealthCheck@SharePoint2010.Dev.local”)
    $headers.Add(“subject”, “SharePoint 2010 Dev – Daily Health Analyzer report”)
    $headers.Add(“content-type”, “text/html”)

    $web = Get-SPWeb $ReportsList.ParentWeb.Url
    [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web, $headers, $bodyText)
    $web.Dispose()

  2. Karthik says:

    Can we not set an alert on the this list with alert me option ?
    Any specific reason to write this code ?

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: