Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
Adastram | eb4509d781 | ||
Adastram | 295a0fe59f |
146
autostart.py
Normal file
146
autostart.py
Normal file
|
@ -0,0 +1,146 @@
|
|||
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"""
|
||||
|
||||
from datetime import datetime
|
||||
user = getlogin()
|
||||
path = join(getcwd(), "taskschedulerxml.xml")
|
||||
date = datetime.now()
|
||||
|
||||
XML = f"""<?xml version="1.0" encoding="UTF-16"?>
|
||||
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
||||
<RegistrationInfo>
|
||||
<URI>{uri}</URI>
|
||||
<SecurityDescriptor>{securitydescriptor}</SecurityDescriptor>
|
||||
<Source>{source}</Source>
|
||||
<Date>{date.year}-{date.month}-{date.day}T{date.hour}:{date.minute}:{date.second}</Date>
|
||||
<Author>{user}</Author>
|
||||
<Version>{version}</Version>
|
||||
<Description>{description}</Description>
|
||||
<Documentation>{documentation}</Documentation>
|
||||
</RegistrationInfo>
|
||||
<Triggers>
|
||||
<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>
|
||||
<LogonTrigger>
|
||||
<Enabled>false</Enabled>
|
||||
<StartBoundary>{startboundary}</StartBoundary>
|
||||
<EndBoundary>{endboundary}</EndBoundary>
|
||||
<Repetition>{repetiton}</Repetition>
|
||||
<ExecutionTimeLimit>{executiontimelimit}</ExecutionTimeLimit>
|
||||
<UserId>{user}</UserId>
|
||||
</LogonTrigger>
|
||||
<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>
|
||||
</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
|
Loading…
Reference in a new issue