如何发送电子邮件系统;应用程序事件记录在同一电子邮件中

时间:2011-02-10 21:10:50

标签: powershell powershell-v2.0

此代码是每天向我发送一封电子邮件中的事件日志错误,但我该如何提交应用程序;系统在一个电子邮件中记录?感谢。

$emailFrom = "admin@company.com"
$emailTo = "user@company.com"
$subject = "Daily Eventlog Errors"
$emailbody = get-eventlog -logname application -entrytype Error, Warning -after (get-date).addHours(-24) -computer (get-content c:\ServerList.txt) |
select MachineName, timewritten, source, message |format-table -auto  | out-string
$message = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $emailbody)
$smtpServer = "companyemail.exchangemail.com"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

2 个答案:

答案 0 :(得分:1)

$emailbody = get-eventlog -logname application -entrytype Error, Warning -after (get-date).addHours(-24) -computer (get-content c:\ServerList.txt) |
select MachineName, timewritten, source, message |format-table -auto  | out-string

$emailbody = $emailbody + " " + get-eventlog -logname system -entrytype Error, Warning -after (get-date).addHours(-24) -computer (get-content c:\ServerList.txt) |
select MachineName, timewritten, source, message |format-table -auto  | out-string

答案 1 :(得分:0)

它不是最新的帖子,但它是我在搜索脚本时起床的那个。

我冒昧地为它添加了自己的触摸,并觉得我应该在这里发布:

$emailFrom = "admin@company.com"
$emailTo = "user@company.com"
$subject = "Daily Eventlog Errors"

$emailbodyAPP = get-eventlog -logname application -entrytype Error, Warning -after (get-date).addHours(-24) -computer (get-content c:\ServerList.txt) | 
select MachineName, timewritten, source, message |format-table -auto  | out-string

$emailbodySRV = get-eventlog -logname system -entrytype Error, Warning -after (get-date).addHours(-24) -computer (get-content c:\ServerList.txt) | 
select MachineName, timewritten, source, message |format-table -auto  | out-string

$emailbody = $emailbodyAPP + $emailbodySRV

$message = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $emailbody)
$smtpServer = "companyemail.exchangemail.com"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)