开发者

how to email system & application eventlogs in the same email

开发者 https://www.devze.com 2023-02-09 16:31 出处:网络
This code is to send me errors of eventlogs in an email everyday, but how do I put application & system logs together in one email?Thanks.

This code is to send me errors of eventlogs in an email everyday, but how do I put application & system logs together in one email? Thanks.

$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-conte开发者_开发百科nt 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)


$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


its not the newest post, but its the one I got up when I searched for a script to do this.

I took the liberty to add my own touch to it, and felt I should post it here:

$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)
0

精彩评论

暂无评论...
验证码 换一张
取 消