1

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.

screenshot

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
brunoto
  • 91
  • 9

1 Answers1

0

The code you were given should work, the only issue is when you changed the TASK_LOGON_NONE type from 0 to 1. You have up to 6 options you can change this too, see this link for a list of values and meanings.

When you supplied a value of 1 you must also supply a password at registration time.

For more information on what values you can use for root_folder.RegisterTaskDefinition, check this Microsoft documentation as well.