Reputation: 77
what i want to do is - log an event in the eventlog - application, only when the scheduled script gives any red error in that session...
I know how to log it with Write-EventLog, but cant limit it to only when it gives a red error saying something maybe wrong.
your response is much appreciated!
Upvotes: 0
Views: 256
Reputation: 200213
One way is to clear $Error
before your statemtent, then check if an error occurred:
$Error.Clear()
Invoke-Something
if ($Error.Count -gt 0) {
Write-EventLog ...
}
Or you could turn all errors into terminating errors and use a try..catch
statement:
$eap = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
try {
Invoke-Something
} catch {
Write-EventLog ...
}
$ErrorActionPreference = $eap
Upvotes: 1