0

I have the following code (thanks to input from 'jon Z' and 'manojlds') that processes server lists, checks the error logs for each server against a search string, and then sends an email report listing the servers with bad error logs and good error logs.

What I would like to do now is do a count of the number of servers with bad error logs and good error logs and put that info in the email report as well.

I would have something like this in each heading of my email report:

The following n servers have bad error logs:

The following n servers are OK:

I've been trying to accomplish this by using the Count method, but have so far been unsuccessful. Is the Count method the correct approach? Is so, how (where would I apply it in my code)? If not, then what is the best approach?

Code:

$BadServerLogs = "<font style=`"font-family:verdana;font-size:9pt`"><p><b>The following servers have bad error logs:</b></p>"
$GoodServerLogs = "<font style=`"font-family:verdana;font-size:9pt`"><p><b>The following servers are OK:</b></p>"

# Use hash table to associate server list to search string array
$Groups = @{
$SERVER_LST_1=$SEARCH_STR_ARRAY_1;
$SERVER_LST_2=$SEARCH_STR_ARRAY_2;
$SERVER_LST_3=$SEARCH_STR_ARRAY_3;
$SERVER_LST_4=$SEARCH_STR_ARRAY_4;
}
$StartupErrors = @{}
$Groups.keys | %{
$key = $_
gc $key | %{
    # Check StartupError.log files for errors
    $StartupErrors[$_] = Get-ChildItem -Path \\$_\$LOG_PATH -Include StartupError.log -Recurse | Select-String -notmatch $Groups["$key"]
    If ($StartupErrors[$_])
    {
        $Subject = "StartupError Logs Report: BAD ERROR LOGS!"
        $BadServerLogs += "<li><a href=`"\\$_\$LOG_PATH\StartupError.log`">$_</a></li>"
    }
    Else 
    { 
        $Subject = "StartupError Logs Report: All Logs are Fine"
        $GoodServerLogs += "<li>$_</li>" 
    }
}   
}
# Send email listing servers with bad/good StartupError log files
Send-MailMessage -Body "$BadServerLogs $GoodServerLogs" -BodyAsHtml -Subject $Subject -SmtpServer $SmtpServer -To $MailTo -From $MailFrom

Thanks in advance! -Keith

2
  • I don't see count in there anywhere? Commented Jan 18, 2012 at 20:10
  • I think he's asking where it would go... The first two lines are where you need the count, but you don't know the count yet. Commented Jan 18, 2012 at 20:12

1 Answer 1

4

You can just create two variables:

$goodCount = 0
$badCount = 0

Then in the loop increment each depending on the case:

If ($StartupErrors[$_])
{
    $Subject = "StartupError Logs Report: BAD ERROR LOGS!"
    $BadServerLogs += "<li><a href=`"\\$_\$LOG_PATH\StartupError.log`">$_</a></li>"
    $badCount += 1
}
Else 
{ 
    $Subject = "StartupError Logs Report: All Logs are Fine"
    $GoodServerLogs += "<li>$_</li>"
    $goodCount += 1
}

Then create some HTML and send it:

$html = "<h1>The following $badCount servers have bad error logs:</h1>" + $BadServerLogs
$html += "<h1>The following $goodCount servers are OK:</h1>" + $GoodServerLogs

Send-MailMessage -Body $html -BodyAsHtml -Subject $Subject -SmtpServer $SmtpServer -To $MailTo -From $MailFrom
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.