Hello,
I have the below powershell script which i'm executing on a server, but when i'm receiving the email in my mailbox, sometimes i have blank emails with only the subject appearing as "EventLog OutputSERVERNAME System Error" or sometimes i get the content of the email but only the servename changes for each machine but not the content.
Please help me with the below script will be greatly appreciated.
param($argument1,$argument2)
$log = $argument1
$log_type = $argument2
$startdate = (get-date).adddays(-7)
$EmailFrom = "<noreply@test.com>"
$EmailTo = "<myaddress@domain.com>"
$SMTPServer = "127.0.0.1"
function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
}
$list = get-content server.txt #Server.txt
file contains the list of all the machines which i'd like to have their logs sent to my email address
foreach ($i in $list) {
$pc = $i.split(" ")
$EmailSubject = "EventLog Output " + $pc + " " + $log + " " + $log_type
$emailbody = Get-EventLog -ComputerName $pc -LogName $log -After $startdate -entrytype $log_type | Convertto-HTML
send_email
}
$pc = ""
$log = ""
$log_type = ""
$startdate = ""
$eventlog = ""
$emailbody = ""
$EmailFrom = ""
$EmailTo = ""
$EmailSubject = ""
$SMTPServer = ""
$list = ""
$i = ""
exit