Imported autostart
This commit is contained in:
parent
8c1c261f34
commit
295a0fe59f
86
autostart.py
Normal file
86
autostart.py
Normal file
|
@ -0,0 +1,86 @@
|
|||
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"""
|
||||
|
||||
|
||||
user = getlogin()
|
||||
path = join(getcwd(), "taskschedulerxml.xml")
|
||||
|
||||
XML = f"""<?xml version="1.0" encoding="UTF-16"?>
|
||||
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
||||
<RegistrationInfo>
|
||||
<Date>2023-11-27T00:00:00</Date>
|
||||
<Author>{user}</Author>
|
||||
</RegistrationInfo>
|
||||
<Triggers>
|
||||
<LogonTrigger>
|
||||
<Enabled>true</Enabled>
|
||||
<UserId>{user}</UserId>
|
||||
</LogonTrigger>
|
||||
</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