SharePoint Diagnostic Log Monitor + Email Alert PowerShell Script

November 29th, 2011 § 2 Comments

Looking for a bootleg SharePoint monitoring and alert system? Well, you’ve come to the right place! I wrote the following PowerShell script to be run as a scheduled task against SharePoint’s Diagnostic Logs. It will not only find the latest log file to analyze, but it will slice and dice through that current log file for the most critical “Critical” events. Once it finds those Critical events that are usually indicative of something really wrong with your environment, it’ll package up all of the messages in an HTML email and send it your way.

You may need to highlight and copy the script below into notepad to be able to read all of the characters that were chopped off by this blog’s layout.

############# Start Variables ################
$logDirectory = "D:\Logs\Diagnostic Logs\*.log"
$emailFrom = "FromEmailAddress"
$emailTo = @("Email1","Email2")
$subject = "SharePoint Diagnostics Critical Alert"
$smtpserver = "EmailServer"
############# End Variables ##################
$latestLogFile = get-childitem $logDirectory | sort LastWriteTime -desc | select -first 1
$criticalItems = Select-String $latestLogFile.FullName -Pattern "Critical"
if($criticalItems -ne $null)
{  
     $body = ""  
     foreach($criticalItem in $criticalItems)  
     {   
         $body += "<b>Error:</b> " + $criticalItem.Line + "<br><br>"   
         $body += "<b>Line Number:</b> " + $criticalItem.LineNumber + "<br><br>"   
         $body += "<b>File Path:</b> " + $criticalItem.Path + "<br><br>"   
         $body += "===================================<br><br>"  
     }    

     Send-MailMessage -To $emailTo -Subject $subject -Body $body -SmtpServer $smtpserver -From $emailFrom -BodyAsHtml
}

 

Advertisement

Tagged: , , ,

§ 2 Responses to SharePoint Diagnostic Log Monitor + Email Alert PowerShell Script

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 )

Connecting to %s

What’s this?

You are currently reading SharePoint Diagnostic Log Monitor + Email Alert PowerShell Script at The SharePoint Swiss Army Knife.

meta

Follow

Get every new post delivered to your Inbox.

Join 585 other followers