1

I am trying to export scheduled tasks information for tracking whether any failed tasks and their timings. There is a function on task scheduler for exporting but is there a way to automate the export using python?

  • All the info is in Mark Hammond's book *Python programming on Win32* ISBN 1-56592-621-8. It's way too big a topic to cover in a SO answer. – BoarGules Jun 30 '19 at 14:17

1 Answers1

1

Looks like this question has an example of pulling the data you are asking about. https://stackoverflow.com/a/36635050/4734574

Update:

I used the first example from the link and got all the tasks setup on my system. If you aren't seeing all of the tasks it could possibly be a permissions issue with UAC (User Account Control). If this is the case you might want to run your dos/git-bash with administrator permissions.

  1. pip install pywin32
  2. run the following python script:

task_dump.py

import win32com.client

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

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

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:
        settings = task.Definition.Settings
        print('Path       : %s' % task.Path)
        print('Hidden     : %s' % settings.Hidden)
        print('State      : %s' % TASK_STATE[task.State])
        print('Last Run   : %s' % task.LastRunTime)
        print('Last Result: %s\n' % task.LastTaskResult)
print('Listed %d tasks.' % n)

Note: I made no modifications from the original posters post.

  1. Run: python task_dump.py > tasks.log

Example Result

Path       : \NvBatteryBoostCheckOnLogon_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-30 10:15:36+00:00
Last Result: 0

Path       : \NvDriverUpdateCheckDaily_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-29 22:25:26+00:00
Last Result: 0

Path       : \NVIDIA GeForce Experience SelfUpdate_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 1999-11-30 00:00:00+00:00
Last Result: 267011

Path       : \NvNgxUpdateCheckDaily_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-29 23:25:31+00:00
Last Result: 1

Path       : \NvNodeLauncher_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-30 10:13:35+00:00
Last Result: -2147467259

Path       : \NvProfileUpdaterDaily_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-29 22:25:22+00:00
Last Result: 0

Path       : \NvProfileUpdaterOnLogon_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-30 10:15:36+00:00
Last Result: 0

Path       : \NvTmMon_{B2FE1952-0186-46C3-BAEC-A80AA35AC5B8}
Hidden     : False
State      : Ready
Last Run   : 2019-06-30 10:15:36+00:00
Last Result: 0
Douglas C.
  • 56
  • 5
  • Thanks, it seems to only get the last result but what I need to pull is all the historical run times, E.g.Task A ran at 30 Jun 1am Successful, 30 Jun 2am Failed, something like that which I can make into a data frame. – user6008722 Jun 30 '19 at 10:10
  • I used the first example and it returned all the tasks status on a Windows 10 system. For the first example you need to install the pywin32 module (`pip install pywin32`) in order for the script to work. – Douglas C. Jun 30 '19 at 14:20
  • Ah just reread your comment. I wasn't understanding the run times for the same task. – Douglas C. Jun 30 '19 at 15:09
  • In order for you to get history you have to have history enabled. If you do have it enabled those are actually Windows Events so you are actually looking for details from the EventLog instead of the TaskScheduler directly. – Douglas C. Jun 30 '19 at 15:54
  • Is there a way to download the details from event log? – user6008722 Jul 01 '19 at 13:57