Tuesday, September 24, 2013

SharePoint Health Analyser automated mail using Powershell

Hi guys,

Got a requirement to send the farm health report to the farm adminstrators group to get the updates automatically throgh the mail.

After some research, I found the basic power shell script which pulls the data from Health Reports list in the central administration web application

I modified and updated the script to get the severity incidents in the HTML tabular form ,

We can execute the script with task scheduler for automation.

#Loading SharePoint Powershell Snaping
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Get central administration web application details
$caWebApp=(Get-SPWebApplication -IncludeCentralAdministration) | ? {$_.IsAdministrationWebApplication -eq $true}
$caWeb=Get-SPWeb -Identity $caWebApp.url;
#Get health reports list
$healthList=$caWeb.GetList("\Lists\HealthReports")
$displayURL=$caWeb.Url + ($healthList.Forms | where {$_.Type -eq "PAGE_DISPLAYFORM"}).ServerRelativeURL
#write-host $displayURL
# Get query results
#$queryString="<Query><Where><Eq><FieldRef Name='HealthReportSeverity'/><Value Type='Text'>2 - Warning</Value></Eq></Where><OrderBy><FieldRef Name='Modified'
Ascending='FALSE' /></OrderBy></Query>"
$queryString = "<Where><And><Neq><FieldRef Name='HealthReportSeverity' /><Value Type='Text'>4 - Success</Value></Neq><Eq><FieldRef Name='Modified'/><Value
Type='DateTime'><Today /></Value></Eq></And></Where><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy>"
$query=New-Object Microsoft.SharePoint.SPQuery
$query.Query=$queryString
#$query.RowLimit=10
$resultItems=$healthList.GetItems($query)
#write-host $resultitems.Count
#Write HTML Table
[Array]$servers=$caWebApp.Farm.Servers
$msgTitle="Health Analyzer results for SharePoint farm "+ $caWebApp.Farm.DisplayName + " "+(Get-Date)
$head = "<style type=`"text/css`">.tableStyle { border: 1px solid #000000; }</style>"
$head=$head+ "<Titel>$msgTitle</Title>"
#$body = "<H2>$msgTitle</H2><table cellspacing=`"0`" class=`"tableStyle`" style=`"width: 100%`">"
#Table for displaying server names
$body = "<H2>$msgTitle</H2><table cellspacing=`"0`" style=`"width: 50%`">"
$servers | ForEach-Object {
$body=$body+"<tr><td>$_</td></tr>" }
$body=$body+"</table>"
$body=$body+"<br/>"
$body=$body+"<br/>"
$body =$body+"<table cellspacing=`"0`" class=`"tableStyle`" style=`"width: 100%`">"
$body =$body+"<tr><th>URL</th><th>Severity</th><th>Category</th><th>Explanation</th><th>Modified Date</th></tr>"
foreach ($item in $resultItems)
{
$itemUrl = $displayURL + "?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 |Out-string
# write to address, from address
$toAddress="" #SPECIFY THE RECIPIENT ADDRESS
$ccAddress="" #SPECIFY COPY ADDRESS
#$shareAddress=$caWebApp.OutboundMailReplyToAddress
$shareAddress="admin@DOMAIN.COM"
$serverAddress=$caWebApp.OutboundMailServiceInstance.Server.Address
#initiating server smtp client intantc
Send-MailMessage -To $toAddress -Cc $ccAddress -From $shareAddress -Body $msgBody -SmtpServer $serverAddress -Subject $msgTitle -BodyAsHtml
#write-host "Mail sent succesfully"+(Get-Date)
view raw gistfile1.txt hosted with ❤ by GitHub

No comments: