powershell - How to log an eventlog only when the script gives a red error? -
what want - log event in eventlog - application, when scheduled script gives red error in session...
i know how log write-eventlog, cant limit when gives red error saying maybe wrong.
your response appreciated!
one way clear $error
before statemtent, check if error occurred:
$error.clear() invoke-something if ($error.count -gt 0) { write-eventlog ... }
or turn errors terminating errors , use try..catch
statement:
$eap = $erroractionpreference $erroractionpreference = 'stop' try { invoke-something } catch { write-eventlog ... } $erroractionpreference = $eap
Comments
Post a Comment