我的PowerShell脚本中有以下代码片段......
我还想做的是发送一个电子邮件报告,列出列表中发现的每个错误日志,并列出错误日志正常的所有服务器。电子邮件正文中的内容如下:
以下服务器的错误日志错误:
以下服务器正常:
这是我的代码段:
$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."
}
}
我猜我需要一个Send-Mail功能,我会在服务器名称中传递错误的错误日志等等。但是,我不太清楚如何处理这个问题。
任何好的想法都将不胜感激。谢谢!
答案 0 :(得分:2)
如果您使用的是Powershell V1,请使用Powershell Cookbook中的this function发送邮件。在Powershell V2中,您可以使用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
备注:在Powershell食谱函数中,smtpserver参数称为smtphost。
答案 1 :(得分:1)
你需要自己创建这个函数,但这里有一些伪代码:
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
所以细分: