The script reads the health analyzer reports and sends an email if and only the severity of the issue is "1 - Error". It sends an email attachment with the error info that needs immediate action. Schedule this script in windows task scheduler and make it run once in a day to get every days report.
Edit the server name, from, to and SMTP addresses for email delivery.
Script to send Health Analyzer Reports
$LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
$LogFile = ".\HealthAnalyserPatch-$LogTime.rtf"
# Add SharePoint PowerShell Snapin
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase
#Deleting any .rtf files in the scriptbase location
$FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
if($FindRTFFile)
{
foreach($file in $FindRTFFile)
{
remove-item $file
}
}
start-transcript $logfile
if ($PSVersionTable) {$Host.Runspace.ThreadOptions = 'ReuseThread'}
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# get the health reports list
$server = "Server Name"
$ReportsList = [Microsoft.SharePoint.Administration.Health.SPHealthReportsList]::Local
$FormUrl = '{0}{1}?id=' -f $ReportsList.ParentWeb.Url, $ReportsList.Forms.List.DefaultDisplayFormUrl
$body = $ReportsList.Items | where {
if($_['Severity'] -eq '1 - Error') {
if (test-path $scriptbase\HealthAnalyserError.htm)
{
remove-item $scriptbase\HealthAnalyserError.htm
}
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-file $scriptbase\HealthAnalyserError.htm
$EmailFrom = "From address"
$EmailTo = "To address"
$EmailSubject = "URGENT!!!! Need attention on Health Anlayser Report in $server server"
$emailbody = "Hi SharePoint Admin Team,
kindly take necessary actions on health analyser errors.
Thanks,
SharePoint Health Analyser Monitoring Script."
$SMTPServer = "SMTP address"
$emailattachment = "$scriptbase\HealthAnalyserError.htm"
function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
$SMTPClient.Send($mailmessage)
$attachment.dispose()
$mailmessage.dispose()
if (test-path $scriptbase\HealthAnalyserError.htm)
{
remove-item $scriptbase\HealthAnalyserError.htm
}
}
send_email
}
}
stop-transcript