我有以下代码(感谢'jon Z'和'manojlds'的输入)处理服务器列表,根据搜索字符串检查每个服务器的错误日志,然后发送一个电子邮件报告列出服务器坏了错误日志和良好的错误日志。
我现在要做的是对错误日志错误和错误日志错误的服务器数量进行计数,并将该信息也放在电子邮件报告中。
我会在电子邮件报告的每个标题中都有这样的内容:
以下 n 服务器的错误日志错误:
以下 n 服务器正常:
我一直试图通过使用Count
方法来实现这一目标,但到目前为止还没有成功。 Count
方法是正确的方法吗?是这样,怎么样(我会在我的代码中将它应用到哪里)?如果没有,那么最好的方法是什么?
代码:
$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
提前致谢! -Keith
答案 0 :(得分:4)
您可以创建两个变量:
$goodCount = 0
$badCount = 0
然后根据情况在循环中递增:
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
}
然后创建一些HTML并发送它:
$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