python 管理员权限运行_Python Windows任务以管理员身份运行

该博客介绍了如何使用Python的COMTaskScheduler接口创建一个计划任务,该任务以管理员权限运行。通过示例代码展示了如何设置任务的执行路径、参数、工作目录,并在完成后隐藏任务。示例任务是启动计算器应用。
摘要由CSDN通过智能技术生成

//description of your code hereUsesthe new COMTaskSchedulerInterfaceto create a new disabled scheduled task,then run it onceaspart of a script.Usethis to launch interactive tasks,even remotely.importwin32com.client

computer_name=""#leave all blank for current computer, current usercomputer_username=""computer_userdomain=""computer_password=""action_id="Test Task"#arbitrary action IDaction_path=r"c:\windows\system32\calc.exe"#executable path (could be python.exe)action_arguments=r''#arguments (could be something.py)action_workdir=r"c:\windows\system32"#working directory for action executableauthor="Someone"#so that end users know who you aredescription="testing task"#so that end users can identify the tasktask_id="Test Task"task_hidden=False#set this to True to hide the task in the interfaceusername=""password=""run_flags="TASK_RUN_NO_FLAGS"#see dict below, use in combo with username/password#define constantsTASK_TRIGGER_DAILY=2TASK_CREATE=2TASK_CREATE_OR_UPDATE=6TASK_ACTION_EXEC=0IID_ITask="{148BD524-A2AB-11CE-B11F-00AA00530503}"RUNFLAGSENUM={"TASK_RUN_NO_FLAGS":0,"TASK_RUN_AS_SELF":1,"TASK_RUN_IGNORE_CONSTRAINTS":2,"TASK_RUN_USE_SESSION_ID":4,"TASK_RUN_USER_SID":8}#connect to the scheduler (Vista/Server 2008 and above only)scheduler=win32com.client.Dispatch("Schedule.Service")scheduler.Connect(computer_nameorNone,computer_usernameorNone,computer_userdomainorNone,computer_passwordorNone)rootFolder=scheduler.GetFolder("\\")#(re)define the tasktaskDef=scheduler.NewTask(0)colTriggers=taskDef.Triggerstrigger=colTriggers.Create(TASK_TRIGGER_DAILY)trigger.DaysInterval=100trigger.StartBoundary="2100-01-01T08:00:00-00:00"#never starttrigger.Enabled=FalsecolActions=taskDef.Actionsaction=colActions.Create(TASK_ACTION_EXEC)action.ID=action_id

action.Path=action_path

action.WorkingDirectory=action_workdir

action.Arguments=action_arguments

info=taskDef.RegistrationInfoinfo.Author=author

info.Description=description

settings=taskDef.Settingssettings.Enabled=Falsesettings.Hidden=task_hidden#register the task (create or update, just keep the task name the same)result=rootFolder.RegisterTaskDefinition(task_id,taskDef,TASK_CREATE_OR_UPDATE,"","",RUNFLAGSENUM[run_flags])#username, password#run the task oncetask=rootFolder.GetTask(task_id)task.Enabled=TruerunningTask=task.Run("")task.Enabled=False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值