Powershell Help Needed Displaying Event Logs

Hello

I wrote a simple powershell script to display only warnings for the past 24 hours and errors for the past 24 hours

This works just like I want  my question is how can I display a message to say "No Events Found"

When no events meet my criteria i get a message see below

Write-Host "Yello" -ForegroundColor Yellow
get-winevent -FilterHashTable @{LogName='Application'; Level=2; StartTime=(get-date).addhours(-24)}
Write-Host "Red" -ForegroundColor Red
get-winevent -FilterHashTable @{LogName='Application'; Level=3; StartTime=(get-date).addhours(-24)}

Open in new window


The output  

PS C:\util> ./get-system
Yello
Get-WinEvent : No events were found that match the specified selection criteria.
At C:\util\get-system.ps1:2 char:13
+ get-winevent <<<<  -FilterHashTable @{LogName='System'; Level=2; StartTime=(get-date).addhours(-24)}
    + CategoryInfo          : ObjectNotFound: (:) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand

Red
Get-WinEvent : No events were found that match the specified selection criteria.
At C:\util\get-system.ps1:4 char:13
+ get-winevent <<<<  -FilterHashTable @{LogName='System'; Level=3; StartTime=(get-date).addhours(-24)}
    + CategoryInfo          : ObjectNotFound: (:) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand

another example

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\util> ./get-application
Yello
Get-WinEvent : No events were found that match the specified selection criteria.
At C:\util\get-application.ps1:2 char:13
+ get-winevent <<<<  -FilterHashTable @{LogName='Application'; Level=2; StartTime=(get-date).addhours(-24)}
    + CategoryInfo          : ObjectNotFound: (:) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand

Red

TimeCreated                   ProviderName                                             Id Message
-----------                   ------------                                             -- -------
11/5/2015 7:21:10 AM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/5/2015 7:21:10 AM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/5/2015 7:21:10 AM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 11:21:09 PM         Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 11:21:09 PM         Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 11:21:09 PM         Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 3:21:08 PM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 3:21:08 PM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 3:21:08 PM          Microsoft-Windows-Certific...                            64 Certificate for local syst...


PS C:\util>


would like to see this instead

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\util> ./get-application
Yello

No Warning Events have been found for today

Red

TimeCreated                   ProviderName                                             Id Message
-----------                   ------------                                             -- -------
11/5/2015 7:21:10 AM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/5/2015 7:21:10 AM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/5/2015 7:21:10 AM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 11:21:09 PM         Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 11:21:09 PM         Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 11:21:09 PM         Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 3:21:08 PM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 3:21:08 PM          Microsoft-Windows-Certific...                            64 Certificate for local syst...
11/4/2015 3:21:08 PM          Microsoft-Windows-Certific...                            64 Certificate for local syst...


PS C:\util>

Thanks

Tom
LVL 23
Thomas GrassiSystems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
When no events are found it generates a non-terminating error.  If you want to display some message, then change it to be a terminating error (using -ErrorAction Stop) and use a try/catch block to catch the error.
try {
    get-winevent -FilterHashTable @{LogName='Application'; Level=2; StartTime=(get-date).addhours(-24)} -ErrorAction Stop
}
catch {
    "No events found"
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas GrassiSystems AdministratorAuthor Commented:
Thanks

Worked just as I needed
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.