#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

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: