Powershell Script to Monitor the Health Analyser Reports

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

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\HealthAnalyserPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.  foreach($file in $FindRTFFile)  
  20.   {  
  21.    remove-item $file  
  22.   }  
  23. }  
  24.   
  25.   
  26. start-transcript $logfile  
  27.   
  28. if ($PSVersionTable) {$Host.Runspace.ThreadOptions = 'ReuseThread'}  
  29. Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
  30.  
  31. # get the health reports list  
  32. $server = "Server Name"  
  33. $ReportsList = [Microsoft.SharePoint.Administration.Health.SPHealthReportsList]::Local  
  34. $FormUrl = '{0}{1}?id=' -f $ReportsList.ParentWeb.Url, $ReportsList.Forms.List.DefaultDisplayFormUrl  
  35. $body = $ReportsList.Items | where {  
  36.   
  37. if($_['Severity'] -eq '1 - Error') {   
  38. if (test-path $scriptbase\HealthAnalyserError.htm)  
  39. {  
  40. remove-item $scriptbase\HealthAnalyserError.htm  
  41. }  
  42.         
  43. New-Object PSObject -Property @{  
  44.   
  45. Url = "<a href='$FormUrl$($_.ID)'>$($_['Title'])</a>"  
  46. Severity = $_['Severity']  
  47. Category = $_['Category']  
  48. Explanation = $_['Explanation']  
  49. Modified = $_['Modified']  
  50. FailingServers = $_['Failing Servers']  
  51. FailingServices = $_['Failing Services']  
  52. Remedy = $_['Remedy']      
  53.   
  54. } | ConvertTo-Html | Out-file $scriptbase\HealthAnalyserError.htm  
  55.   
  56. $EmailFrom = "From address"  
  57.  $EmailTo = "To address"  
  58.  $EmailSubject = "URGENT!!!! Need attention on Health Anlayser Report in $server server"  
  59. $emailbody = "Hi SharePoint Admin Team,  
  60.    
  61. kindly take necessary actions on health analyser errors.  
  62.   
  63. Thanks,  
  64. SharePoint Health Analyser Monitoring Script."    
  65.     
  66. $SMTPServer = "SMTP address"  
  67.    
  68. $emailattachment = "$scriptbase\HealthAnalyserError.htm"  
  69.    
  70. function send_email {  
  71.  $mailmessage = New-Object system.net.mail.mailmessage   
  72. $mailmessage.from = ($emailfrom)   
  73. $mailmessage.To.add($emailto)  
  74.  $mailmessage.Subject = $emailsubject  
  75.  $mailmessage.Body = $emailbody  
  76.    
  77. $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')  
  78.    $mailmessage.Attachments.Add($attachment)  
  79.   
  80.  
  81. #$mailmessage.IsBodyHTML = $true  
  82.  $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)    
  83. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername""$SMTPAuthPassword")  
  84.  $SMTPClient.Send($mailmessage)  
  85. $attachment.dispose()  
  86. $mailmessage.dispose()  
  87. if (test-path $scriptbase\HealthAnalyserError.htm)  
  88. {  
  89. remove-item $scriptbase\HealthAnalyserError.htm  
  90. }  
  91.  }  
  92. send_email  
  93.   
  94. }  
  95. }  
  96. stop-transcript