Prevent $error variable from populating on PowerShell 2

I have a script which writes all errors contained in $Error to a log. One the steps it performs is run Get-WinEvent to check for a specific event ID. Hopefully in 99% of cases, it won’t find this event. When the event isn’t found though, an error is generated and the $error variable is populated with failure. I know newer versions of PowerShell can use -erroraction ignore but I have some devices that will run this using PowerShell 2 which can’t use ignore. I’d considered using -ErrorVariable and writing that in my log but excluding the Get-WinEvent function from writing to that variable. Any other ideas on how to prevent Get-WinEvent from populating the $Error variable in PS 2?

submitted by /u/grayfold3d
[link] [comments]

Leave a Reply