none
Powershell for error in log file

    Question

  • Hi, Is there a powershell script which will send immediate email alert if there is a specific error message in SharePoint 2010 ULS log(aavaailable in Windows server 2008 in desired drive)?

    I need to run a powershell command every minute and send email alert if there is a specific error message in log. The logs are generating in  every 30 minutes and each  log file have different name, but they do exists in same folder.

    Please help me!


    Santosh sethi

    Friday, February 08, 2013 7:42 PM

Answers

  • Thanks, now I followed your instruction and wrote a windows powershell command as:

    ##search error in ULS log
    if (Select-String -Path D:\logs\*.txt -pattern "Name of that error")

    {

     function sendMail{

         Write-Host "Sending Email"

         #SMTP server name
         $smtpServer = "smtp.domain.com"

         #Creating a Mail object
         $msg = new-object Net.Mail.MailMessage

         #Creating SMTP server object
         $smtp = new-object Net.Mail.SmtpClient($smtpServer)

         #Email structure
         $msg.From = "Sharepoint@domain.com"
         $msg.ReplyTo = "santosh.sethi@domain.com"
         $msg.To.Add("santosh.sethi@domain.com")
         $msg.subject = "Error in ULS log"
         $msg.body = "please check there is a known error in ULS log."

         #Sending email
         $smtp.Send($msg)
     
    }

    #Calling function
    sendMail

    }

    else
          {write-host "nothing to process"}

    ======================================

    Is it right one? I believe there are few script difference between SharePoint powershell and windows powershell


    Santosh sethi


    Unless sharepoint re-defines standard cmdlets (which seems unlikely) standard powershell should work there - but I do not work in that environment.

    But, rather than asking me if it is correct, why not just run it to see if it does what you think it should?

    One suggestion, though, the function should be defined before the first executable statement in the script (i.e. the first IF).


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, February 11, 2013 1:41 AM

All replies

  • I managed the following scripts:
    ##Get the error in ULS log
     Get-SPLogEvent | ?{$_.error -eq “specific error name”}   
    ##Check if error is expected one of not
    if ($error -eq $"specific error name")
    {
          send-mailmessage -subject "error in log" -from sharepoint@toro.com -to santosh.sethi@toro.com `
          -smtpserver vscan.toro.com
    }
          # If no files exist, do nothing
          else
          {write-host "nothing to process"}

    Not sure about it accuracy.


    Santosh sethi

    Friday, February 08, 2013 7:59 PM
  • there could be such a script, however, you would probably have to write it yourself. to get you started you will need info about a couple of key cmdlets:

        help get-eventlog -detailed
        help send-mailmessage -detailed

    once you have figured out how to use those cmdlets and get a rudimentary script running, you will need to deal with a few issues regarding running powershell scripts as scheduled tasks. For that you could start here: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/be251286-3483-4ef9-8f6b-f6328102d897

    note that your suggested logic contains a flaw. If a specific error message exists in the log and your script sends the alert message, it will keep doing so every time it is run as long as the error message remains in the log. You could include additional logic to prevent that, but that is a problem for farther down the road.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Friday, February 08, 2013 8:09 PM
  • This is what I use to receive an email notification when I find the word "ERROR" in a log. I set it to run on a scheduled task and it works like a charm.

    $fail = Get-Content "\\server\c$\folder\file.log" | Select-String "ERROR" -quiet -casesensitive
    if ($fail -eq "True"){Send-MailMessage -From "you@yourmail.com" -To "you@yourmail.com"-Subject "Hey, your stuff failed." -Body "Your stuff failed real bad." -Attachments "\\server\c$\folder\file.log" -smtpServer outlookclient.yourmail.com}
    

    Friday, February 08, 2013 8:22 PM
  • Thanks, now I followed your instruction and wrote a windows powershell command as:

    ##search error in ULS log
    if (Select-String -Path D:\logs\*.txt -pattern "Name of that error")

    {

     function sendMail{

         Write-Host "Sending Email"

         #SMTP server name
         $smtpServer = "smtp.domain.com"

         #Creating a Mail object
         $msg = new-object Net.Mail.MailMessage

         #Creating SMTP server object
         $smtp = new-object Net.Mail.SmtpClient($smtpServer)

         #Email structure
         $msg.From = "Sharepoint@domain.com"
         $msg.ReplyTo = "santosh.sethi@domain.com"
         $msg.To.Add("santosh.sethi@domain.com")
         $msg.subject = "Error in ULS log"
         $msg.body = "please check there is a known error in ULS log."

         #Sending email
         $smtp.Send($msg)
     
    }

    #Calling function
    sendMail

    }

    else
          {write-host "nothing to process"}

    ======================================

    Is it right one? I believe there are few script difference between SharePoint powershell and windows powershell


    Santosh sethi


    Friday, February 08, 2013 8:35 PM

  • Thanks for your responses, I will test and will let you know the update.

    Santosh sethi

    Friday, February 08, 2013 8:58 PM
  • Santosh, my apologies. I assumed you were talking about event logs, not log files.

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, February 11, 2013 1:37 AM
  • Thanks, now I followed your instruction and wrote a windows powershell command as:

    ##search error in ULS log
    if (Select-String -Path D:\logs\*.txt -pattern "Name of that error")

    {

     function sendMail{

         Write-Host "Sending Email"

         #SMTP server name
         $smtpServer = "smtp.domain.com"

         #Creating a Mail object
         $msg = new-object Net.Mail.MailMessage

         #Creating SMTP server object
         $smtp = new-object Net.Mail.SmtpClient($smtpServer)

         #Email structure
         $msg.From = "Sharepoint@domain.com"
         $msg.ReplyTo = "santosh.sethi@domain.com"
         $msg.To.Add("santosh.sethi@domain.com")
         $msg.subject = "Error in ULS log"
         $msg.body = "please check there is a known error in ULS log."

         #Sending email
         $smtp.Send($msg)
     
    }

    #Calling function
    sendMail

    }

    else
          {write-host "nothing to process"}

    ======================================

    Is it right one? I believe there are few script difference between SharePoint powershell and windows powershell


    Santosh sethi


    Unless sharepoint re-defines standard cmdlets (which seems unlikely) standard powershell should work there - but I do not work in that environment.

    But, rather than asking me if it is correct, why not just run it to see if it does what you think it should?

    One suggestion, though, the function should be defined before the first executable statement in the script (i.e. the first IF).


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, February 11, 2013 1:41 AM