Python 检查已完成和失败的任务 Windows 调度程序 [英] Python check for Completed and failed Task Windows scheduler

查看:47
本文介绍了Python 检查已完成和失败的任务 Windows 调度程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我可以查看的方法或资源,以便能够检查我在任务计划程序中拥有的所有 Windows 任务的状态?我想看看任务是失败还是成功.我想在 Python 中执行此操作.

Does anyone know of a way or a resource I can look at to be able to check the status of all my Windows tasks I have in the task scheduler? I would like to see if that see if the task failed or was successful. I would like to do this in Python.

我对使用 win32com.client 模块进行了一些研究.我可以看到任务是什么,但无法找到已完成作业的状态.

I have looked a little at using the win32com.client module. I can see what tasks are but can't find what the status of completed job are.

import win32com.client
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
tasks = scheduler.GetRunningTasks(1)
names = [tasks.Item(i+1).Name for i in range(tasks.Count)]
print names

推荐答案

以下使用Task Scheduler API 打印所有已注册任务的基本信息,包括上次运行时间和结果.

The following uses the Task Scheduler API to print basic information for all registered tasks, including the last run time and result.

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)

这仅从列表中的根文件夹开始.每次通过循环都会弹出一个文件夹;推送其所有子文件夹;并列出文件夹中的任务.它一直持续到文件夹列表为空.

This starts with only the root folder in the list. Each pass through the loop pops a folder; pushes all of its subfolders; and lists the tasks in the folder. It continues until the list of folders is empty.

COM 接口

或者,这里有一个 walk_tasks 生成器,它以标准库的 os.walk 为模型.

Alternatively, here's a walk_tasks generator that's modeled on the standard library's os.walk.

import os
import pywintypes
import win32com.client

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

def walk_tasks(top, topdown=True, onerror=None, include_hidden=True,
               serverName=None, user=None, domain=None, password=None):
    scheduler = win32com.client.Dispatch('Schedule.Service')
    scheduler.Connect(serverName, user, domain, password)
    if isinstance(top, bytes):
        if hasattr(os, 'fsdecode'):
            top = os.fsdecode(top)
        else:
            top = top.decode('mbcs')
    if u'/' in top:
        top = top.replace(u'/', u'\\')
    include_hidden = TASK_ENUM_HIDDEN if include_hidden else 0
    try:
        top = scheduler.GetFolder(top)
    except pywintypes.com_error:
        if onerror is not None:
            onerror(error)
        return
    for entry in _walk_tasks_internal(top, topdown, onerror, include_hidden):
        yield entry


def _walk_tasks_internal(top, topdown, onerror, flags):
    try:
        folders = list(top.GetFolders(0))
        tasks = list(top.GetTasks(flags))
    except pywintypes.com_error as error:
        if onerror is not None:
            onerror(error)
        return

    if not topdown:
        for d in folders:
            for entry in _walk_tasks_internal(d, topdown, onerror, flags):
                yield entry

    yield top, folders, tasks

    if topdown:
        for d in folders:
            for entry in _walk_tasks_internal(d, topdown, onerror, flags):
                yield entry

示例

if __name__ == '__main__':
    n = 0
    for folder, subfolders, tasks in walk_tasks('/'):
        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)

这篇关于Python 检查已完成和失败的任务 Windows 调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆