2023-11-28 10:58:56 +00:00
|
|
|
from os import remove, getlogin, getcwd
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
from os.path import join
|
|
|
|
|
|
|
|
|
|
|
|
def add_to_scheduler(foldername: str, appname: str, programpath: str, arguments: str = ""):
|
|
|
|
"""Add the task to the task scheduler\n
|
|
|
|
The task is triggered at the user who executed this function login\n
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
* folder_name\n
|
|
|
|
Define the folder of the task scheduler in which we're going to create our task
|
|
|
|
* app_name\n
|
|
|
|
Define the name of the task in the task scheduler
|
|
|
|
* program_path\n
|
|
|
|
Define the program to execute when task is triggered\n
|
|
|
|
* arguments\n
|
|
|
|
The arguments to pass to the program executed when task is triggered
|
|
|
|
Return
|
|
|
|
------
|
|
|
|
True if command created the task successfully, else, return False"""
|
|
|
|
|
2023-12-06 21:20:20 +00:00
|
|
|
from datetime import datetime
|
2023-11-28 10:58:56 +00:00
|
|
|
user = getlogin()
|
|
|
|
path = join(getcwd(), "taskschedulerxml.xml")
|
2023-12-06 21:20:20 +00:00
|
|
|
date = datetime.now()
|
|
|
|
|
2023-11-28 10:58:56 +00:00
|
|
|
XML = f"""<?xml version="1.0" encoding="UTF-16"?>
|
|
|
|
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
|
|
|
<RegistrationInfo>
|
2023-12-06 21:20:20 +00:00
|
|
|
<URI>{uri}</URI>
|
|
|
|
<SecurityDescriptor>{securitydescriptor}</SecurityDescriptor>
|
|
|
|
<Source>{source}</Source>
|
|
|
|
<Date>{date.year}-{date.month}-{date.day}T{date.hour}:{date.minute}:{date.second}</Date>
|
2023-11-28 10:58:56 +00:00
|
|
|
<Author>{user}</Author>
|
2023-12-06 21:20:20 +00:00
|
|
|
<Version>{version}</Version>
|
|
|
|
<Description>{description}</Description>
|
|
|
|
<Documentation>{documentation}</Documentation>
|
2023-11-28 10:58:56 +00:00
|
|
|
</RegistrationInfo>
|
|
|
|
<Triggers>
|
2023-12-06 21:20:20 +00:00
|
|
|
<BootTrigger>
|
|
|
|
<Enabled>false</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</BootTrigger>
|
|
|
|
<RegistrationTrigger>
|
|
|
|
<Enabled>false</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</RegistrationTrigger>
|
|
|
|
<IdleTrigger>
|
|
|
|
<Enabled>false</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</IdleTrigger>
|
|
|
|
<TimeTrigger>
|
|
|
|
<Enabled>false</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</TimeTrigger>
|
|
|
|
<EventTrigger>
|
|
|
|
<Enabled>false</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</EventTrigger>
|
2023-11-28 10:58:56 +00:00
|
|
|
<LogonTrigger>
|
2023-12-06 21:20:20 +00:00
|
|
|
<Enabled>false</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
2023-11-28 10:58:56 +00:00
|
|
|
<UserId>{user}</UserId>
|
|
|
|
</LogonTrigger>
|
2023-12-06 21:20:20 +00:00
|
|
|
<SessionStateChangeTrigger>
|
|
|
|
<Enabled>{logontrigger}</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</SessionStateChangeTrigger>
|
|
|
|
<CalendarTrigger>
|
|
|
|
<Enabled>{calendartrigger}</Enabled>
|
|
|
|
<StartBoundary>{startboundary}</StartBoundary>
|
|
|
|
<EndBoundary>{endboundary}</EndBoundary>
|
|
|
|
<Repetition>{repetiton}</Repetition>
|
|
|
|
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
|
|
|
</CalendarTrigger>
|
2023-11-28 10:58:56 +00:00
|
|
|
</Triggers>
|
|
|
|
<Principals>
|
|
|
|
<Principal id="Author">
|
|
|
|
<UserId>{user}</UserId>
|
|
|
|
<LogonType>InteractiveToken</LogonType>
|
|
|
|
<RunLevel>LeastPrivilege</RunLevel>
|
|
|
|
</Principal>
|
|
|
|
</Principals>
|
|
|
|
<Settings>
|
|
|
|
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
|
|
|
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
|
|
|
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
|
|
|
<AllowHardTerminate>true</AllowHardTerminate>
|
|
|
|
<StartWhenAvailable>false</StartWhenAvailable>
|
|
|
|
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
|
|
|
<IdleSettings>
|
|
|
|
<StopOnIdleEnd>true</StopOnIdleEnd>
|
|
|
|
<RestartOnIdle>false</RestartOnIdle>
|
|
|
|
</IdleSettings>
|
|
|
|
<AllowStartOnDemand>true</AllowStartOnDemand>
|
|
|
|
<Enabled>true</Enabled>
|
|
|
|
<Hidden>false</Hidden>
|
|
|
|
<RunOnlyIfIdle>false</RunOnlyIfIdle>
|
|
|
|
<WakeToRun>false</WakeToRun>
|
|
|
|
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
|
|
|
|
<Priority>7</Priority>
|
|
|
|
</Settings>
|
|
|
|
<Actions Context="Author">
|
|
|
|
<Exec>
|
|
|
|
<Command>{programpath}</Command>
|
|
|
|
<Arguments>{arguments}</Arguments>
|
|
|
|
</Exec>
|
|
|
|
</Actions>
|
|
|
|
</Task>"""
|
|
|
|
|
|
|
|
#Write the file so we can execute it later
|
|
|
|
with open(path, "w+") as f:
|
|
|
|
f.write(XML)
|
|
|
|
|
|
|
|
#Execute the command
|
|
|
|
process = Popen(["schtasks", "/create", "/XML", f'"{path}"', "/TN", f'{foldername}\\{appname}', "/F"], stderr=PIPE)
|
|
|
|
|
|
|
|
#Remove the file
|
|
|
|
remove(path)
|
|
|
|
|
|
|
|
#If no error output, return true, else, return false
|
|
|
|
if process.stderr.read() == b'':
|
|
|
|
return True
|
|
|
|
return False
|