Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scheduled Task Error Notification
#2
Function GetScheduledTaskStatus
Code:
Copy      Help
;/
function# $taskName [DATE&nextRunTime] [DATE&lastRunTime] [str&statusString] [int&lastExitCode] [int&isRunning]

;Gets some information about a scheduled task.
;Returns 1 if successful (although some variables may be emty if fais to retrieve), or 0 if task does not exist or cannot be opened.


;nextRunTime - next run time or 0 if not scheduled.
;lastRunTime - last run time or 0.
;statusString - if the task could not be started, contains error description. If ok, empty.
;lastExitCode - program's exit code or 0.
;isRunning - 1 if the task is curently running, 0 if not.


;Use 0 for arguments you don't need.

;Reference: search for ITask in http://msdn.microsoft.com/library


;def SCHED_S_TASK_READY 0x00041300
def SCHED_S_TASK_RUNNING 0x00041301
;def SCHED_S_TASK_DISABLED 0x00041302
def SCHED_S_TASK_HAS_NOT_RUN 0x00041303
;def SCHED_S_TASK_NO_MORE_RUNS 0x00041304
;def SCHED_S_TASK_NOT_SCHEDULED 0x00041305
;def SCHED_S_TASK_TERMINATED 0x00041306
;def SCHED_S_TASK_NO_VALID_TRIGGERS 0x00041307
;def SCHED_S_EVENT_TRIGGER 0x00041308

interface ITaskScheduler :IUnknown SetTargetComputer GetTargetComputer Enum Activate Delete NewWorkItem AddWorkItem IsOfType {148BD527-A2AB-11CE-B11F-00AA00530503}
interface IScheduledWorkItem :IUnknown CreateTrigger DeleteTrigger GetTriggerCount GetTrigger GetTriggerString GetRunTimes GetNextRunTime SetIdleWait GetIdleWait Run Terminate EditWorkItem GetMostRecentRunTime GetStatus GetExitCode SetComment GetComment SetCreator GetCreator SetWorkItemData GetWorkItemData SetErrorRetryCount GetErrorRetryCount SetErrorRetryInterval GetErrorRetryInterval SetFlags GetFlags SetAccountInformation GetAccountInformation
interface ITask :IScheduledWorkItem SetApplicationName GetApplicationName SetParameters GetParameters SetWorkingDirectory GetWorkingDirectory SetPriority GetPriority SetTaskFlags GetTaskFlags SetMaxRunTime GetMaxRunTime {148BD524-A2AB-11CE-B11F-00AA00530503}
;
ITaskScheduler ts._create("{148BD52A-A2AB-11CE-B11F-00AA00530503}")
ITask task

BSTR bName=taskName
int hr=ts.Activate(bName uuidof(ITask) &task); if(hr) ret

SYSTEMTIME st
if(&lastRunTime)
,hr=task.GetMostRecentRunTime(&st)
,if(hr) lastRunTime=0; else lastRunTime.fromsystemtime(st)

if(&nextRunTime)
,hr=task.GetNextRunTime(&st)
,if(hr) nextRunTime=0; else nextRunTime.fromsystemtime(st)

if(&lastExitCode or &statusString)
,if(!&lastExitCode) &lastExitCode=_i
,lastExitCode=0
,hr=task.GetExitCode(&lastExitCode)
,if(&statusString)
,,sel(hr)
,,,case [0,SCHED_S_TASK_HAS_NOT_RUN] statusString.all
,,,case else statusString.dllerror("" "" hr)

if(&isRunning)
,hr=task.GetStatus(&isRunning)
,isRunning=isRunning=SCHED_S_TASK_RUNNING

ret 1

Examples
Code:
Copy      Help
ClearOutput

str name="notepad"

DATE nextrun lastrun
str statusstr
int exitcode
int isrunning

GetScheduledTaskStatus name nextrun lastrun statusstr exitcode isrunning

out nextrun
out lastrun
out statusstr
out exitcode
out isrunning

Code:
Copy      Help
str s
DATE d
if(!GetScheduledTaskStatus("task name" 0 d s)) end "task does not exist"
if(s.len)
,out "could not start, %s" s
else
,out "successfully started at %s" _s.from(d)


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)