Sending SharePoint CA system notification e-mails using PowerShell

The script in this article demonstrates an example of how you could use PowerShell to send system notification e-mails to administrators on a scheduled basis. Of course, there are a number of ways to do this sort of thing, including SharePoint workflows, Microsoft SCOM, and custom code. I’m not saying PowerShell is necessarily better than these methods, but I like the flexibility it brings, and as you have full access to the Object Model, you could send notifications from all sorts of areas – for example, successes and errors from the crawl logs, information from the User Profile service application, timer jobs with last run times, list items, site users, etc.

The example I have chosen here is to send an HTML e-mail listing the currently active items from the SharePoint Health Analyzer. This information is effectively just stored in a SharePoint list on the Central Administration site, so I will need to access the list, query the active items and send this information using the SMTP mail server and reply addresses specified in the farm outbound e-mail settings.

    Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
    $caWebApp = (Get-SPWebApplication -IncludeCentralAdministration) | ? { $_.IsAdministrationWebApplication -eq $true }
$caWeb = Get-SPWeb -Identity $caWebApp.Url
    #Set up from, to and server addresses
$toAddress = “user@domain.pl”
$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 = “<Where><Neq><FieldRef   Name=’HealthReportSeverity’ /><Value Type=’Text’>4 – Success</Value></Neq></Where>”
$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 = “<style type=`”text/css`”>.tableStyle { border: 1px solid     #000000; }</style>”
$head = $head + “<Title>$msgTitle</Title>”
#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[“Ważność”], $item[“Kategoria”],    $item[“Wyjaśnienie”], $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
   #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()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s