I've been trying to set a python script than will create a Windows Task Schedule based on some parameters. But first, I'm running some tests to be sure that I can set it up in the way it is needed to work.
As a base, I've found the question Is there a way to add a task to the windows task scheduler via python 3? here on stackoverflow and it shows a code that works really good. However, I need to be able to set up the option "Run whether user is logged on or not", like the image below.
I've tried some options, like setting a user and password in the configuration and changing the task logon parameters, but it didn't work.
root_folder.RegisterTaskDefinition(
"TEST", # Task name
task_def,
TASK_CREATE_OR_UPDATE,
'myUsername',
'myUserPassword',
1)
Traceback:
Traceback (most recent call last):
File "c:\Users\myUsername\Documents\VSCode\GUITest\windscheduler.py", line 38, in <module>
root_folder.RegisterTaskDefinition(
File "<COMObject GetFolder>", line 3, in RegisterTaskDefinition
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147023570), None)
This happens whether I change the last parameters from RegisterTaskDefinition
from 0 to 1. I've tried to run this from Administrator PowerShell but it didn't work either.
The full working code is in the link before, I've only changed what is in the code snippet.