TASKNAME=r"test"  #specify your exact task name between the double quotes
ps_command=r'SCHTASKS /Query /TN "'+TASKNAME+'" /FO list /v'
import subprocess
import ctypes
import re
import sys
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
info="""
0 - The operation completed successfully.
1 - Incorrect function called or unknown function called. 2 File not found.
10 - The environment is incorrect. 
267008 - Task is ready to run at its next scheduled time. 
267009 - Task is currently running. 
267010 - The task will not run at the scheduled times because it has been disabled. 
267011 - Task has not yet run. 
267012 - There are no more runs scheduled for this task. 
267013 - One or more of the properties that are needed to run this task on a schedule have not been set. 
267014 - The last run of the task was terminated by the user. 
267015 - Either the task has no triggers or the existing triggers are disabled or not set. 
2147750671 - Credentials became corrupted. 
2147750687 - An instance of this task is already running. 
2147943645 - The service is not available (is "Run only when an user is logged on" checked?). 
3221225786 - The application terminated as a result of a CTRL+C. 
3228369022 - Unknown software exception.
267016 - Event triggers do not have set run times.
2147549186 - Call was canceled by the message filter.
2147750665 - A task's trigger is not found.
2147750666 - One or more of the properties required to run this task have not been set.
2147750667 - There is no running instance of the task.
2147750668 - The Task Scheduler service is not installed on this computer.
2147750669 - The task object could not be opened.
2147750670 - The object is either an invalid task object or is not a task object.
2147750671 - No account information could be found in the Task Scheduler security database for the task indicated.
2147750672 - Unable to establish existence of the account specified.
2147750673 - Corruption was detected in the Task Scheduler security database.
2147750674 - Task Scheduler security services are available only on Windows NT.
2147750675 - The task object version is either unsupported or invalid.
2147750676 - The task has been configured with an unsupported combination of account settings and run time options.
2147750677 - The Task Scheduler Service is not running.
2147750678 - The task XML contains an unexpected node.
2147750679 - The task XML contains an element or attribute from an unexpected namespace.
2147750680 - The task XML contains a value which is incorrectly formatted or out of range.
2147750681 - The task XML is missing a required element or attribute.
2147750682 - The task XML is malformed.
267035 - The task is registered, but not all specified triggers will start the task.
267036 - The task is registered, but may fail to start. Batch logon privilege needs to be enabled for the task principal.
2147750685 - The task XML contains too many nodes of the same type.
2147750686 - The task cannot be started after the trigger end boundary.
2147750687 - An instance of this task is already running.
2147750688 - The task will not run because the user is not logged on.
2147750689 - The task image is corrupt or has been tampered with.
2147750690 - The Task Scheduler service is not available.
2147750691 - The Task Scheduler service is too busy to handle your request. Please try again later.
2147750692 - The Task Scheduler service attempted to run the task, but the task did not run due to one of the constraints in the task definition.
267045 - The Task Scheduler service has asked the task to run.
2147750694 - The task is disabled.
2147750695 - The task has properties that are not compatible with earlier versions of Windows.
2147750696 - The task settings do not allow the task to start on demand.
3221225786 - The application terminated as a result of a CTRL+C.
3221225794 - The application failed to initialize properly.
"""
def extractMax(input): 
    numbers = re.findall('\d+',input)
    if numbers:
        return numbers[0]
    else:
        return "Error excuting script"
         
        
def alert(arg):
    sys.stderr.write("%d%d%d" % (arg, arg, arg))

def output(result,info):
    z = re.findall("Last Result:(.*)",result)
    if z:
        spc=z[0].replace(" ","")
        #print spc
        if int(spc) in [1,10,267013,267014,267015,2147750671,3228369022,214794364,3221225794,
                       2147750696,2147750695,2147750692,2147750691,2147750690,2147750689,
                       2147750688,2147750680,2147750677,2147750673,2147750672,2147750670,
                       267010,2147943645,2147750665,2147750666,2147750669]:
            for i in info.splitlines():
                num=extractMax(i)
                spc=spc.replace("\r","")
                if str(num) == str(spc):
                    print "Task scheduler error:"+i
                    alert(1)
            
        else:
            for i in info.splitlines():
                #print i
                num=extractMax(i)
                spc=spc.replace("\r","")
                if str(num) == str(spc):
                    print "Task scheduler success:"+i
                    alert(0)
                


with disable_file_system_redirection():
    process=subprocess.Popen(ps_command, shell=True, stdout=subprocess.PIPE)
result=process.communicate()
ret=process.returncode
if ret==0:
    output(result[0],info)
    #print result[0]
else:
    print "Error excuting the script"
    alert(0)
