Get-Eventlog not showing results

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of StephenC StephenC 5 months, 2 weeks ago.

  • Author
    Posts
  • #43123
    Profile photo of StephenC
    StephenC
    Participant

    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 5 months, 2 weeks ago by Profile photo of Don Jones Don Jones.
  • #43301
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #43459
    Profile photo of StephenC
    StephenC
    Participant

    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"
    	}
    
  • #43463
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #43465
    Profile photo of StephenC
    StephenC
    Participant

    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.

  • #43476
    Profile photo of StephenC
    StephenC
    Participant

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

    • This reply was modified 5 months, 2 weeks ago by Profile photo of StephenC StephenC.
  • #43489
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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

  • #43491
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #43493
    Profile photo of StephenC
    StephenC
    Participant

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

You must be logged in to reply to this topic.