salt icon indicating copy to clipboard operation
salt copied to clipboard

[BUG] `win_task` datetime formatting errors

Open jtraub91 opened this issue 3 years ago • 4 comments

Description For win_task, some of the formatting specifications from the doc do not work and will throw an error. Perhaps this code block is part of the problem.

Setup Install salt on Windows

Steps to Reproduce the behavior Run a command similar to what is shown in the docs

C:\Windows\system32>salt-call --local task.create_task test_task cmd="echo hello world" trigger_type=Once start_date=2022-09-20 start_time=10:15

But receive an error

Passed invalid arguments: strptime() argument 1 must be str, not int.

Usage:

    Create a new task in the designated location. This function has many keyword
    arguments that are not listed here. For additional arguments see:

        - :py:func:`edit_task`
        - :py:func:`add_action`
        - :py:func:`add_trigger`

    Args:

        name (str):
            The name of the task. This will be displayed in the task scheduler.

        location (str):
            A string value representing the location in which to create the
            task. Default is ``\`` which is the root for the task scheduler
            (``C:\Windows\System32\tasks``).

        user_name (str):
            The user account under which to run the task. To specify the
            'System' account, use 'System'. The password will be ignored.

        password (str):
            The password to use for authentication. This should set the task to
            run whether the user is logged in or not, but is currently not
            working.

        force (bool):
            If the task exists, overwrite the existing task.

    Returns:
        bool: ``True`` if successful, otherwise ``False``

    CLI Example:

    .. code-block:: bash

        salt 'minion-id' task.create_task <task_name> user_name=System force=True action_type=Execute cmd='del /Q /S C:\\Temp' trigger_type=Once start_date=2016-12-1 start_time=01:00

Expected behavior When using the first format indicated in the docs for start_time (%I:%H:%S %p instead of %H:%M) it works

C:\Windows\system32>salt-call --local task.create_task test_task cmd="echo hello world" trigger_type=Once start_date=2022-09-20 start_time="10:15:00 AM"
local:
    True

and can observe the created task in Windows Task Scheduler

image

Versions Report

C:\Windows\system32>salt-call --versions-report
Salt Version:
          Salt: 3005

Dependency Versions:
          cffi: 1.14.6
      cherrypy: 18.6.1
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: 4.0.7
     gitpython: 3.1.18
        Jinja2: 3.1.0
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: 1.1.4
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.21
      pycrypto: Not Installed
  pycryptodome: 3.10.1
        pygit2: Not Installed
        Python: 3.8.13 (tags/v3.8.13:ea67321, Jul 12 2022, 09:32:58) [MSC v.1916 64 bit (AMD64)]
  python-gnupg: 0.4.8
        PyYAML: 5.4.1
         PyZMQ: 19.0.0
         smmap: 4.0.0
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.2

System Versions:
          dist:
        locale: cp1252
       machine: AMD64
       release: 10
        system: Windows
       version: 10 10.0.19041 SP0 Multiprocessor Free

jtraub91 avatar Sep 20 '22 18:09 jtraub91

Note
Arguments are parsed by the YAML loader and are subject to yaml's idiosyncrasies. Therefore, time values in some formats (%H:%M:%S and %H:%M) should to be quoted. See YAML IDIOSYNCRASIES for more details.

That includes arguments passed on the CLI.

OrangeDog avatar Sep 21 '22 10:09 OrangeDog

@OrangeDog I don't believe quoting changed the behavior

jtraub91 avatar Sep 21 '22 18:09 jtraub91

@OrangeDog Confirmed quoting does not fix this bug. Please see output below.

C:\Windows\system32>salt-call --local task.create_task test_task cmd="echo hello world" trigger_type=Once start_date=2022-09-22 start_time="10:15"

Passed invalid arguments: strptime() argument 1 must be str, not int.

Usage:

    Create a new task in the designated location. This function has many keyword
    arguments that are not listed here. For additional arguments see:

        - :py:func:`edit_task`
        - :py:func:`add_action`
        - :py:func:`add_trigger`

    Args:

        name (str):
            The name of the task. This will be displayed in the task scheduler.

        location (str):
            A string value representing the location in which to create the
            task. Default is ``\`` which is the root for the task scheduler
            (``C:\Windows\System32\tasks``).

        user_name (str):
            The user account under which to run the task. To specify the
            'System' account, use 'System'. The password will be ignored.

        password (str):
            The password to use for authentication. This should set the task to
            run whether the user is logged in or not, but is currently not
            working.

        force (bool):
            If the task exists, overwrite the existing task.

    Returns:
        bool: ``True`` if successful, otherwise ``False``

    CLI Example:

    .. code-block:: bash

        salt 'minion-id' task.create_task <task_name> user_name=System force=True action_type=Execute cmd='del /Q /S C:\\Temp' trigger_type=Once start_date=2016-12-1 start_time=01:00

jtraub91 avatar Sep 21 '22 18:09 jtraub91

That was shell quoting. You also need to have quotes in the YAML value.

"start_time='10:15'"

OrangeDog avatar Sep 21 '22 19:09 OrangeDog

@OrangeDog ah ok, that got it to work. That's a weird one; oddly, single quotes work (start_time='10:00') but double quotes do not (start_time="10:00"). This may not be a bug after all, but, I will note that the CLI example (for create_task) shown in the docs and error output does not have the correct syntax. Perhaps that can be updated, if possible.

jtraub91 avatar Sep 22 '22 17:09 jtraub91

It depends on the quoting rules for the shell you're using. In bash you need both.

OrangeDog avatar Sep 22 '22 17:09 OrangeDog