繁体   English   中英

如何使用 Python 和 win32com 访问 Windows 调度程序“操作”

[英]How to access Windows Scheduler "Actions" with Python and win32com

我正在尝试获取 Windows 调度程序中任务的操作。 Eryk Sun's excellent code snippet Python check for Completed and failed Task Windows scheduler is "almost" enough to get me the action associated with the task, but I'm stumped trying to interpret Microsofts documentation https://docs.microsoft.com/en -us/windows/win32/api/taskschd/并将其转换为 win32com.client 将接受的内容。 我可以提取的样本:

 import win32com.client
 scheduler = win32com.client.Dispatch('Schedule.Service')
 scheduler.Connect()
 folders = [scheduler.GetFolder('\\')]
 while folders:
    folder = folders.pop(0)
    folders += list(folder.GetFolders(0))
    for task in folder.GetTasks(0):
        print('Name       : %s' % task.Name)
        print('Path       : %s' % task.Path)
        print('Last Run   : %s' % task.LastRunTime)
        print('Last Result: %s\n' % task.LastTaskResult)

我缺少的是如何获取与每个任务相关联的“操作”

有更多的箍要跳过。 MS 文档链接包含所有信息,但有点简洁。 您还必须比平时更深入地研究 win32com。

GetTasks(0)返回一个IRegisteredTask接口,它本身有一个属性Definition ,它返回一个ITaskDefinition接口。 Actions属性返回一个IActionsCollection接口,它是IAction接口的集合。 呸!

不幸的是, IAction 是一个基本的 class ,除了告诉你动作的Type外,它并没有做太多的事情。 派生接口有 4 种类型,其中最常见的是IExecAction ,然后是IComHandlerActionIEmailActionIShowMessageAction 最后两个似乎没有在我的系统上使用,所以我没有测试它们。

下一步取决于您如何使用win32com.client :早期绑定 ( win32com.client.gencache.EnsureDispatch() ) 或后期绑定 ( win32com.client.dynamic.Dispatch() )。

如果使用 early-binding ,则需要IAction 接口转换为 Type 给出的特定操作接口(使用win32com.client.CastTo() )。 如果您使用后期绑定,那么您只需调用特定于类型的方法并希望获得最好的结果。 我个人的偏好是早期绑定,因为它加强了类型安全,并允许您使用常量定义而不是magic numbers

早期绑定代码:

import win32com.client

scheduler = win32com.client.gencache.EnsureDispatch('Schedule.Service')

def PrintAction(action):
    ty = action.Type

    if ty == win32com.client.constants.TASK_ACTION_COM_HANDLER: #=5
        print("COM Handler Action")
        coma = win32com.client.CastTo(action,"IComHandlerAction")
        print(coma.ClassId,coma.Data)
    elif ty == win32com.client.constants.TASK_ACTION_EXEC: #=0
        print("Exec Action")
        execa = win32com.client.CastTo(action,"IExecAction")
        print(execa.Path,execa.Arguments)
    elif ty == win32com.client.constants.TASK_ACTION_SEND_EMAIL: #=6 This might not work
        print("Send Email Action") 
        maila = win32com.client.CastTo(action,"IEmailAction")
        print(maila.Subject,maila.To)
    elif ty == win32com.client.constants.TASK_ACTION_SHOW_MESSAGE: #=7
        print("Show Message Action")
        showa = win32com.client.CastTo(action,"IShowMessageAction")
        print(showa.Title,showa.MessageBody)
    else:
        print("Unknown Action Type!") #Don't expect this


scheduler.Connect()
folders = [scheduler.GetFolder('\\')]
while folders:
    folder = folders.pop(0)
    folders += list(folder.GetFolders(0))
    for task in folder.GetTasks(0):
        print('Name       : %s' % task.Name)
        print('Path       : %s' % task.Path)
        print('Last Run   : %s' % task.LastRunTime)
        print('Last Result: %s' % task.LastTaskResult)
        defn = task.Definition
        actions = defn.Actions
        for action in actions:
            PrintAction(action)
            print()

后期绑定代码:

以不同的方式创建scheduler object,并替换PrintAction() function:

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

def PrintAction(action):
    ty = action.Type

    if ty == 5: 
        print("COM Handler Action")
        print(action.ClassId,action.Data)
    elif ty == 0: 
        print("Exec Action")
        print(action.Path,action.Arguments)
    elif ty == 6: # This might not work
        print("Send Email Action") 
        print(action.Subject,action.To)
    elif ty == 7: 
        print("Show Message Action")
        print(action.Title,action.MessageBody)
    else:
        print("Unknown Action Type!")

以 output 条目之一为例:

Name       : Retry
Path       : \Microsoft\Windows\Management\Provisioning\Retry
Last Run   : 1999-11-30 00:00:00+00:00
Last Result: 267011
Exec Action
%windir%\system32\ProvTool.exe /turn 5 /source ProvRetryTask

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM