Sharepoint Powershell get analyzer reports as email

#Get Central Admin Web Application and Web objects

$caWebApp = (Get-SPWebApplication -IncludeCentralAdministration) | ? { $_.IsAdministrationWebApplication -eq $true }

$caWeb = Get-SPWeb -Identity $caWebApp.Url

#Set up from, to and server addresses

$toAddress = “jayaraja.jayaraman@awacservices.com”

$fromAddress = $caWebApp.OutboundMailReplyToAddress

$serverAddress = $caWebApp.OutboundMailServiceInstance.Server.Address

#Get Health Analyzer list on Central Admin site

$healthList = $caWeb.GetList(“\Lists\HealthReports”)

$displayFormUrl = $caWeb.Url + ($healthList.Forms | where { $_.Type -eq “PAGE_DISPLAYFORM” }).ServerRelativeUrl

$queryString = “4 – Success”

$query = New-Object Microsoft.SharePoint.SPQuery

$query.Query = $queryString

$items = $healthList.GetItems($query)

#Set up e-mail message subject and HTML body

$msgTitle = “Health Analyzer results for farm ” + $caWebApp.Farm.Name + ” – ” + (Get-Date)

#HTML head

$head = “”

$head = $head + “$msgTitle”

#Create HTML body by walking through each item and adding it to a table

$body = “<H2>$msgTitle</H2><table cellspacing=`”0`” class=`”tableStyle`” style=`”width: 100%`”>”

foreach ($item in $items)

{

$itemUrl = $displayFormUrl + “?id=” + $item.ID

[array]$itemValues = @($item[“Severity”], $item[“Category”], $item[“Explanation”], $item[“Modified”])

$body = $body + “”

$body = $body + “” + $item.Title + “

$itemValues | ForEach-Object {

$body = $body + “$_”

}

$body = $body + “”

}

$body = $body + “</table>”

#Create message body using the ConvertTo-Html PowerShell cmdlet

$msgBody = ConvertTo-Html -Head $head -Body $body

#Create e-mail message object using System.Net.Mail class

$msg = New-Object System.Net.Mail.MailMessage($fromAddress, $toAddress, $msgTitle, $msgBody)

$msg.IsBodyHtml = $true

#Send message

$smtpClient = New-Object System.Net.Mail.SmtpClient($serverAddress)

$smtpClient.Send($msg)

$caWeb.Dispose()

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami