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""" {uri} {securitydescriptor} {source} {date.year}-{date.month}-{date.day}T{date.hour}:{date.minute}:{date.second} {user} {version} {description} {documentation} false {startboundary} {endboundary} {repetiton} {executiontimelimit} false {startboundary} {endboundary} {repetiton} {executiontimelimit} false {startboundary} {endboundary} {repetiton} {executiontimelimit} false {startboundary} {endboundary} {repetiton} {executiontimelimit} false {startboundary} {endboundary} {repetiton} {executiontimelimit} false {startboundary} {endboundary} {repetiton} {executiontimelimit} {user} {logontrigger} {startboundary} {endboundary} {repetiton} {executiontimelimit} {calendartrigger} {startboundary} {endboundary} {repetiton} {executiontimelimit} {user} InteractiveToken LeastPrivilege IgnoreNew false false true false false true false true true false false false PT0S 7 {programpath} {arguments} """ #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