History

 Aug   September 2010   Oct

SMTWTFS
   1  2  3  4
  5  6  7  8  91011
12131415161718
19202122232425
2627282930 
Blog Behavioral Synthesis

Syndicate

Advertisements

Collect Scheduled Task Info With Python PDF Print E-mail
Saturday, 28 March 2009

If you ever needed to see if a Window's scheduled task failed for reporting purposes like I do then read on... I wrote this script because some weekends we get failed tasks, still tracking that down, and I want to see them all quickly and easily when I get into the office. I may actually write a script to rerun the tasks that fail at some point. Currently I simply report these in a web page, but you can just as easily send an e-mail, add it to a log or reporting application. You can also schedule tasks by adding the switches to do so without much work and it will collect this inforamtion off remote servers/workstations. 

By the time I get this script finished and polished it should prove to be a great resource and I hope someone can make use of this example. Have fun!  

'''
SCHTASKS.EXE /Create [/S system [/U username [/P [password]]]][/RU username [/RP [password]]
/SC schedule [/MO modifier] [/D day] [/M months] [/I idletime] /TN taskname /TR taskrun
[/ST starttime] [/RI interval] [ {/ET endtime | /DU duration} [/K] [/XML xmlfile] [/V1]] [/SD startdate]
[/ED enddate] [/IT] [/Z] [/F]

'''


import os

def get_tasks(server='',usern='',passwd=''):
    if server:
        cmd = 'SCHTASKS.EXE /s %s /U %s /P %s /Query /v /fo LIST'%(server,usern,passwd)
    else:
        cmd = 'SCHTASKS.EXE /Query /v /fo LIST'

    # Run the command and return results.
    err,res = os.popen2(cmd)
    tasks = res.readlines()

    # Build our dict.
    schedule = {}
    fields_list = ['scheduled task state','last result','task to run','next run time','comment']
    for item in tasks:
        item = item.replace('\n','')
        if not item:
            continue
        try:
            field,content = item.split(': ')
            field = field.strip().lower()
            content = content.strip().lower()
            
            if 'taskname' in field:
                taskname = content
                    if content not in schedule:
                        schedule[content]={}
            if field in fields_list:
                schedule[taskname]
                    [field]=content
        except:
             if field in fields_list:
                 schedule[taskname][field]=content
        return schedule

results = get_tasks()
print(results)

 
Next >
Images ©Copyright 2009-2011 C. Nichols. Hosting by Dreamhost

download joomla cms download joomla cms