Is there a possibility to query the "Run Duration" of a running scheduled Task in Python?

时间:2019-03-19 14:48:13

标签: python windows scheduled-tasks pywin32 win32com

We are trying to query all scheduled Tasks with python. Check the Run Duration - and if a scheduled Task is running longer than 5 hours, the script should send a mail via an e-mail distributor.

We've already written a Function that will send Mails if the “SendMail”-Function is called. In the following Snippet we replaced the “SendMail”-Function with a Print-Statement to focus on our Problem.

Is there a possibility to query the Run Duration of the scheduled-task? The Task Scheduler can display via „Display All Running Tasks“ a Window where the Run Duration is shown.

Link to a picture of the Run Duration, we try to query

import win32com.client


def check_tasks():
    start = 1
    MonTask = ["all"]

    if start == 1:
        TASK_ENUM_HIDDEN = 1
        TASK_STATE = {0: 'Unknown',
                  1: 'Disabled',
                  2: 'Queued',
                  3: 'Ready',
                  4: 'Running'}

        scheduler = win32com.client.Dispatch('Schedule.Service')
        scheduler.Connect()

        longtasklist = []
        LastRun = 0
        n = 0
        folders = [scheduler.GetFolder('\\')]
        while folders:

            folder = folders.pop(0)
            folders += list(folder.GetFolders(0))
            tasks = list(folder.GetTasks(TASK_ENUM_HIDDEN))
            n += len(tasks)

            for task in tasks:
                if (task.name in MonTask) or (MonTask[0] == "all"):
                    settings = task.Definition.Settings 
                    taskname = task.name
                    taskstate = TASK_STATE[task.State]
                    if taskstate == "Running":
                        #################################
                        #if Task Run Duration > 05:00:00#
                        #################################
                            longtasklist.append(taskname)

        if len(longtasklist) > 0:
            print(longtasklist) #only for checking in the console
            MailText = "The Task/s \" %s \" exceeded the time limit" % longtasklist
            print("Send_Mail")
            #Send_Mail(MailText)

check_tasks()

0 个答案:

没有答案
相关问题