0

So if we use the following to create a scheduled task in powershell

Register-ScheduledJob -Name TestJob3 -ScriptBlock { 
    throw "This is an ugly error that should be visible" 
} -Credential $cred

...and then run this task.

The task scheduler reports success.

So we can slightly improve on this by changing the scriptblock to something like the following

try{
    throw "This is an ugly error that should be visible" 
} catch {
    Exit -1
}

Now if you look very carefully you can see that the task scheduler is reporting a last run result of something other than 0 (0xFFFFFFF). However all we have in the history tab is bunch of infomation logging saying that the task and action have completed.

What I would really like is to get some big loud exception level logging in the history view preferably with the nice descriptive error that has been thrown.

Anyone know how this can be achieved?

Alexis Coles
  • 1,227
  • 14
  • 19

1 Answers1

0

Unfortunately, it looks like Task Scheduler doesn't respect exit codes. As far as it is concerned, running the program and seeing it exit is a success. So logging your exceptions will probably need to be done either manually or through Write-EventLog.

Out of curiosity, since this is a ScheduledJob instead of a ScheduledTask, does running Get-ScheduledJob show that the job completed without errors also?

Community
  • 1
  • 1
RiverHeart
  • 549
  • 6
  • 15