Author Posts

June 15, 2016 at 4:21 pm

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 topic was modified 2 years, 3 months ago by  Don Jones.

June 15, 2016 at 8:51 pm

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."

June 15, 2016 at 9:08 pm

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"
	}

June 15, 2016 at 9:14 pm

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.

June 15, 2016 at 9:15 pm

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.

June 15, 2016 at 9:18 pm

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

  • This reply was modified 2 years, 3 months ago by  StephenC.

June 15, 2016 at 9:49 pm

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.

June 15, 2016 at 10:08 pm

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