Get-Eventlog not showing results

Hello Team,

When I execute the following script, it does not show results, only the “else” statement. But when I execute this line by itself, I get several results. It’s obviously something wrong with the script, but since I’m a PS newbie, I’m not sure what it is! Help would be greatly appreciated.

Thanks,
Stephen C

$smtpServer = “smtp.mydomain.com
$smtpFrom = “$env:computername@mydomain.com
$smtpTo = “stephen@mydomain.com
$messageSubject = “$env:computername Application Event log ‘Error/Warning’ in last 24 hours”

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$message.Body = Get-Eventlog -LogName Application -EntryType Error,Warning -After (Get-Date).AddHours(-24) | ConvertTo-Html
ConvertTo-HTML -Body “$message” -Title “messageSubject” -CssURI c:\style.css

If ($message.Body -eq “Error,Warning”)
{
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
}
else
{
Write-host “No Application Events on $env:computername”
}

This:

$message.Body = Get-Eventlog -LogName Application -EntryType Error,Warning -After (Get-Date).AddHours(-24) | ConvertTo-Html

Means $message.Body contains a bunch of HTML.

This:

If ($message.Body -eq "Error,Warning")

Will never be true. $message.Body will never be exactly equal to “Error,Warning.” The -eq operator tests for “exactly equal to.”

Thank you for your response. I removed “Warning” from the code (as shown below) and still get the following issue.

If I execute this line by itself, if details several results:

Get-Eventlog -LogName Application -EntryType Warning -After (Get-Date).AddDays(-10)

When I execute that line in the following script, it only displays the “else” statement:

$smtpServer = "smtp.mds.com"
$smtpFrom = "$env:computername@mdsnocad.com"
$smtpTo = "stephen.crowell@bettervideo.com"
$messageSubject = "$env:computername Application Event log 'Error/Warning' in last 24 hours"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$message.Body = Get-Eventlog -LogName Application -EntryType Warning -After (Get-Date).AddDays(-10) | ConvertTo-Html
ConvertTo-HTML -Body "$message" -Title "messageSubject" -CssURI c:\style.css

If ($message.Body -eq "Warning")
{	
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($message)
    	}
else
{
    Write-host "No Application Events on $env:computername"
	}

Yes, that’s because your block of HTML is never going to be just, and only, the word “Warning.”

It may CONTAIN the word “Warning,” but that isn’t what -eq is looking for.

Why don’t you help me understand what you’re trying to accomplish? Maybe I can offer a suggestion.

What I’m trying to do is have an email notification sent (with the details) if there were any errors in the Application log during last 24 hours. And if there were none, to not send any email notification.

I tried to use -contains instead of -eq, but it still didn’t work.

Ha!! Correct! See if this helps you understand why, and what to do instead: https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/contains-isnt-like.html

But in terms of your actual goal…

$events = Get-Eventlog -LogName Application -EntryType Warning -After (Get-Date).AddDays(-10)
if ( ($events | Measure).count -gt 0 ) {
 # events has stuff
 $message.body = $events | ConvertTo-HTML
 ...etc...
} else {
 ...whatever...
}

Something like that. Rather than converting everything to HTML and then checking for some text within it, why not just see if Get-EventLog actually returned any records? Convert it to HTML if it does, and send your message.

Thank you, Don. That’s exactly what I was needing. I will also take a look at the previous link you suggested.