[英]Python windows task to run with admin
I am looking for some Python code to create a Windows Task in the Task Scheduler, it needs to run at start & have the highest permissions level (admin).我正在寻找一些 Python 代码来在任务计划程序中创建一个 Windows 任务,它需要在启动时运行并具有最高权限级别(管理员)。
// description of your code here
Uses the new COM Task Scheduler Interface to create a new disabled scheduled task, then run it once as part of a script. Use this to launch interactive tasks, even remotely.
import win32com.client
computer_name = "" #leave all blank for current computer, current user
computer_username = ""
computer_userdomain = ""
computer_password = ""
action_id = "Test Task" #arbitrary action ID
action_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 executable
author = "Someone" #so that end users know who you are
description = "testing task" #so that end users can identify the task
task_id = "Test Task"
task_hidden = False #set this to True to hide the task in the interface
username = ""
password = ""
run_flags = "TASK_RUN_NO_FLAGS" #see dict below, use in combo with username/password
#define constants
TASK_TRIGGER_DAILY = 2
TASK_CREATE = 2
TASK_CREATE_OR_UPDATE = 6
TASK_ACTION_EXEC = 0
IID_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_name or None, computer_username or None, computer_userdomain or None, computer_password or None)
rootFolder = scheduler.GetFolder("\\")
#(re)define the task
taskDef = scheduler.NewTask(0)
colTriggers = taskDef.Triggers
trigger = colTriggers.Create(TASK_TRIGGER_DAILY)
trigger.DaysInterval = 100
trigger.StartBoundary = "2100-01-01T08:00:00-00:00" #never start
trigger.Enabled = False
colActions = taskDef.Actions
action = colActions.Create(TASK_ACTION_EXEC)
action.ID = action_id
action.Path = action_path
action.WorkingDirectory = action_workdir
action.Arguments = action_arguments
info = taskDef.RegistrationInfo
info.Author = author
info.Description = description
settings = taskDef.Settings
settings.Enabled = False
settings.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 once
task = rootFolder.GetTask(task_id)
task.Enabled = True
runningTask = task.Run("")
task.Enabled = False
This code creates a task to run daily, but not at login & not as admin.这段代码创建了一个每天运行的任务,但不是在登录时,也不是以管理员身份运行。 Is there any way I can do this without requiring the UAC Prompt to open at startup?有什么方法可以做到这一点,而无需在启动时打开 UAC 提示?
EDIT: I am NOT looking to just make the program ask for administrator, as the prompt will popup, as specified above.编辑:我不希望只是让程序要求管理员,因为提示会弹出,如上所述。 I need it to have the highest execution level in the WINDOWS EVENT SCHEDULER to run at logon.我需要它在 WINDOWS EVENT SCHEDULER 中具有最高执行级别才能在登录时运行。
I think it would be a good idea for you to build a Batch file running python and your script.我认为构建一个运行 python 和脚本的批处理文件对您来说是个好主意。 Having done that there are a few things that you might want to try, i haven't personally had the time to try them out but here you go:完成后,您可能想尝试一些事情,我个人还没有时间尝试,但您可以这样做:
.BAT
file and make it an .exe
once you do that i think you should be able to involve your task scheduler into the mix and get your code running whenever you need.编译.BAT
文件并使其成为.exe
后,我认为您应该能够将您的任务调度程序纳入混合并在您需要时运行您的代码。Do look it up before you execute as this is code I have found elsewhere, but it should do the trick:请在执行之前查看它,因为这是我在其他地方找到的代码,但它应该可以解决问题:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\{E2F13B98-650F-47DB-845A-420A1ED34EC7}User\Software\Microsoft\Windows\CurrentVersion\Policies\Associations]
"LowRiskFileTypes"=".exe;.bat;.cmd;.vbs"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Associations]
"LowRiskFileTypes"=".exe;.bat;.cmd;.vbs"
After searching Google more thoroughly, I came across a command that does exactly what I need.在更彻底地搜索谷歌之后,我遇到了一个完全符合我需要的命令。
schtasks.exe /create /S COMPUTERNAME /RU "NT AUTHORITY\SYSTEM" /RL HIGHEST /SC ONLOGON /TN "Administrative OnLogon Script" /TR "cscript.exe \"Path\To\Script.vbs\""
All I need to do is replace the computer name, and program to execute and it makes the task perfectly.我需要做的就是替换计算机名称和要执行的程序,它使任务完美无缺。 I can execute it in a elevated Python environment to create it.我可以在提升的 Python 环境中执行它来创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.