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 }
Ahh the simlicity of it – very nice. I was thinking that – Pattern check might be quite costly – would be nice if you could set two trace logs – one with just critical errors with which the timer job works – could also Set-SPLogLevel but then miss out on logging other than critical . …
Anyway thanks – will try this some time.
Thanks for the script, may you please help me know if there is a way where i can extarct logs from all the server within a time span?
i like to avoid to go in each server to to extarct the log file, i have explored Merge-SPLogFile but it can on extarct from latest log file