2

I have the following code snippet from my PowerShell script that...

  • Loops through a list of servers
  • Does a Select-String -notmatch on the error log at each server
  • Flags the server if the error log is bad, and gives the OK if the error log if fine

What I'd like to also do is send an email report that enumerates each of the discovered bad error logs in a list, and also list all the servers whose error logs are OK. Something like this in the email body:

The following servers have bad error logs:

  • Server3
  • Server6
  • Server14

The following servers are OK:

  • Server1
  • Server2
  • Server5

Here is my code snippet:

$Servers = Get-Content $ServerLst
ForEach ($Server in $Servers)
{
   $ErrorLog = Get-ChildItem -Path \\$Server\$LOG_PATH -Include Error.log -Recurse | Select-String -notmatch $SEARCH_STR
   If ($ErrorLog)
   {
    Write-Host "Bad Error Log found at $Server!"
   }
   Else
   {
    Write-Host "Error log is OK."           
   }
}

I'm guessing I would need a Send-Mail function where I would pass in the server names with bad error logs, etc. However, I'm not quite sure how to approach this.

Any great ideas will be greatly appreciated. Thanks!

2 Answers 2

2

If you are using Powershell V1 use this function from the Powershell Cookbook to send mail. In Powershell V2 you can send mail using Send-MailMessage.

$Servers = Get-Content $ServerLst
$Bad = "The following servers have bad error logs:`n`n"
$OK = "`nThe following servers are OK:`n`n"
ForEach ($Server in $Servers)
{
   $ErrorLog = Get-ChildItem -Path \\$Server\$LOG_PATH -Include Error.log -Recurse | Select-String -notmatch $SEARCH_STR
   If ($ErrorLog)
   {
    $Bad += "`t - $Server`n"
   }
   Else
   {
    $OK += "`t - $Server`n"           
   }
}
Send-MailMessage -Body "$Bad $OK" -Subject "Bad Logs" -SmtpServer $servername -To $to -From $from  

Remark: The smtpserver parameter is called smtphost in the Powershell cookbook function.

3
  • Thanks! This looks like a clean approach. I will give it a try when I return to work.
    – Keith
    Jan 15, 2012 at 22:12
  • 1
    Hi Jon Z, just to follow up...I've tried your solution and it works great so far! Thanks again!!
    – Keith
    Jan 17, 2012 at 2:11
  • Another question. I want to take this a step further and specify the numbers of servers that have bad error logs and the number of servers that are OK. So $Bad would be something like "The following n servers that have bad error logs:". And $OK would be.."The following n servers are OK:". How would I do this? I think I would need to do a count but not sure where to do the count.
    – Keith
    Jan 18, 2012 at 0:14
1

You will need to make the function on your own, but here is some pseudo code:

Function SendMail
{
    Param(...your params here)

    ...send the mail...


}

<...

Your code to check all your servers

You need to save your errors or issues to an array or hashtable.  
I'll assume you use a 2-field array called $ErrArray

...>

# Now at the end you build a string for the body of the email to incorporate your errors

$StrBody = "Bad Error Log Report`n`n"

$ErrArray | ForEach-Object {$StrBody = $Strbody + "`n$($_[0]) server had an issue: $($_[1])`n"}

SendMail $EmailTo $EmailSubject $StrBody

So the breakdown:

  • Make a mail function
  • Save the results of your analysis to an array or hashtable
  • Iterate through your results object and append each result record to the string for your email
  • Call the email function
2
  • Hi JNK, thanks for the feedback! I was thinking of almost the exact same thing you described above. Just wanted to get an expert's opinion. I'll give your approach a try and post back later...
    – Keith
    Jan 13, 2012 at 20:26
  • I've used this in some larger scripts where I send exception reports for when things don't process correctly and it works great. You should also make sure to use appropriate error trapping to a log file since email procedures can (and often do) fail because of issues outside your script.
    – JNK
    Jan 13, 2012 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.