Hi,I have written a Powershell script for my Windows server where it will search for a particular event for last 1 hr and will send the email.Script is working fine but it is sending the email in case of no error detection also with a blank entry.I want no email in case of no issues. Please help as I am very new in Powershell.Below is the script which I created.#####################################################Function<\/span> SendEevntLogDetails{ param ( $strFilePath ) $MailBody=“Hi Team, Following is the list of errors for the Custom Logs of Stream & Disbursement Portal.” $Logs= Get-Content “$strFilePath\\CustomLogs.txt” $ $head= “$style” $MachineName=“xxxxxxxxx” foreach($log in $Logs) { $tableBOdy= “$styleServerNameSourceEntryTypeEventIdMessage” $MailBody+= “Machine Name:$MachineName EventLog:$log \" $EntryType=@(“Error”) $EventlOGS= Get-EventLog -LogName $log -EntryType $EntryType -After (Get-Date).AddHours(-1) -InstanceID 998 # Get the event Log Details foreach($Event in $EventlOGS) { $Entry=$Event.EntryType $EventId=$Event.EventID $Message=$Event.Message if($Entry -eq “Error”) { $Message=”$Message\" } $Source=$Event.Source $tableBOdy += “$MachineName$Source$Entry$EventId$Message” } $tableBOdy=$tableBOdy+“” $html=“$head$tableBOdy” $MailBody += $html $MailBody += “Thank YouRegards,Admin Team” } [string]$Subject= “Stream & Disbursement Portal Event log Notification” [string<\/span>]$users = “[email protected]<\/a>” [string]$fromemail = “[email protected]<\/a>” [string]$server = “XXXXXXXX” [string]$SMTPserver = “XXXXXXXXX” send-mailmessage -from $fromemail -to $users -subject $Subject -BodyAsHTML $MailBody -priority High -smtpServer $SMTPserver Write-Host “Mail Sent successfully…!!!” } #####################################################<\/p>","upvoteCount":3,"answerCount":9,"datePublished":"2020-08-04T14:02:32.000Z","author":{"@type":"Person","name":"neeraj21","url":"https://community.spiceworks.com/u/neeraj21"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi,I have written a Powershell script for my Windows server where it will search for a particular event for last 1 hr and will send the email.Script is working fine but it is sending the email in case of no error detection also with a blank entry.I want no email in case of no issues. Please help as I am very new in Powershell.Below is the script which I created.#####################################################Function<\/span> SendEevntLogDetails{ param ( $strFilePath ) $MailBody=“Hi Team, Following is the list of errors for the Custom Logs of Stream & Disbursement Portal.” $Logs= Get-Content “$strFilePath\\CustomLogs.txt” $ $head= “$style” $MachineName=“xxxxxxxxx” foreach($log in $Logs) { $tableBOdy= “$styleServerNameSourceEntryTypeEventIdMessage” $MailBody+= “Machine Name:$MachineName EventLog:$log \" $EntryType=@(“Error”) $EventlOGS= Get-EventLog -LogName $log -EntryType $EntryType -After (Get-Date).AddHours(-1) -InstanceID 998 # Get the event Log Details foreach($Event in $EventlOGS) { $Entry=$Event.EntryType $EventId=$Event.EventID $Message=$Event.Message if($Entry -eq “Error”) { $Message=”$Message\" } $Source=$Event.Source $tableBOdy += “$MachineName$Source$Entry$EventId$Message” } $tableBOdy=$tableBOdy+“” $html=“$head$tableBOdy” $MailBody += $html $MailBody += “Thank YouRegards,Admin Team” } [string]$Subject= “Stream & Disbursement Portal Event log Notification” [string<\/span>]$users = “[email protected]<\/a>” [string]$fromemail = “[email protected]<\/a>” [string]$server = “XXXXXXXX” [string]$SMTPserver = “XXXXXXXXX” send-mailmessage -from $fromemail -to $users -subject $Subject -BodyAsHTML $MailBody -priority High -smtpServer $SMTPserver Write-Host “Mail Sent successfully…!!!” } #####################################################<\/p>","upvoteCount":3,"datePublished":"2020-08-04T14:02:32.000Z","url":"https://community.spiceworks.com/t/email-alert-for-event-logs/771385/1","author":{"@type":"Person","name":"neeraj21","url":"https://community.spiceworks.com/u/neeraj21"}},{"@type":"Answer","text":"
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n