Repository: ansible/ansible

Issue:
win_scheduled_task
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report
 - Feature Idea
 - Documentation Report

##### COMPONENT NAME
win_scheduled_task:

##### ANSIBLE VERSION

```
ansible 2.3.0 
 config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---

-->

##### OS / ENVIRONMENT
<!---
Ubuntu 16.04.2 LTS / Windows Server 2012
-->

##### SUMMARY
expected behaviour: creates a runnable Task that also runs once (exp. after Installation of Windows updates and a reboot ist required -> schedule reboot to 3am once), there is no way to add Options to a command that should have to be executed.
The command "shutdown -r -t 1" won't be executed by Windows, because the "-r -t 1" Option isn't set as command Option. The System will fail with error code 0x80070002 (can't find file specified).
Also missing is the "once" Option by Script Default

##### STEPS TO REPRODUCE
<!---
just try to schedule a Task to reboot a Windows Server to a specific time
-->

<!--- Paste example playbooks or commands between quotes below -->
```
    - name: Configure | Plan | Reboot
      win_scheduled_task: name="Ansible Reboot System" execute="shutdown -r -t 1" frequency="once" time="4am" description="Scheduled System Reboot by ANSIBLE" enable=yes state=present user=SYSTEM

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
reboot Server

##### ACTUAL RESULTS
Windows Error Code 0x80070002 during Manual or scheduled execution of that task



Discussion:
cc @petemounce
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I don't use scheduled tasks myself but would you be able to put the shutdown command you want to run inside a .ps1 or .bat file and then schedule running the .bat or .ps1 file instead?

needs_info
I think the `argument:` parameter is meant to be used for adding options to the executable in `execute:`.

IMHO the options of this module are awkward and do not follow the parameter names used by other modules. Maybe we can change the default options and add the old ones as aliases. I would got for `executable:` and `arguments:`.

And for running something *once*, I would go for implementing `frequency: once`. Should not be that hard. I will prepare a PR if you are willing to test.

Resolving patch:
diff --git a/lib/ansible/modules/windows/win_scheduled_task.py b/lib/ansible/modules/windows/win_scheduled_task.py
--- a/lib/ansible/modules/windows/win_scheduled_task.py
+++ b/lib/ansible/modules/windows/win_scheduled_task.py
@@ -40,7 +40,6 @@
   description:
     description:
       - The description for the scheduled task
-    required: false
   enabled:
     description:
       - Enable/disable the task
@@ -58,30 +57,27 @@
   user:
     description:
       - User to run scheduled task as
-    required: false
-  execute:
+  executable:
     description:
       - Command the scheduled task should execute
-    required: false
-  argument:
+    aliases: [ execute ]
+  arguments:
     description:
       - Arguments to provide scheduled task action
-    required: false
+    aliases: [ argument ]
   frequency:
     description:
       - The frequency of the command, not idempotent
-    required: false
     choices:
+      - once
       - daily
       - weekly
   time:
     description:
       - Time to execute scheduled task, not idempotent
-    required: false
   days_of_week:
     description:
       - Days of the week to run a weekly task, not idempotent
-    required: false
   path:
     description:
       - Task folder in which this task will be stored
@@ -92,12 +88,13 @@
 # Create a scheduled task to open a command prompt
 - win_scheduled_task:
     name: TaskName
-    execute: cmd
-    frequency: daily
-    time: 9am
     description: open command prompt
+    executable: cmd
+    arguments: -opt1 -opt2
     path: example
-    enable: yes
+    time: 9am
+    frequency: daily
     state: present
+    enabled: yes
     user: SYSTEM
 '''


Test patch:
diff --git a/test/integration/targets/win_scheduled_task/aliases b/test/integration/targets/win_scheduled_task/aliases
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/win_scheduled_task/aliases
@@ -0,0 +1 @@
+windows/ci/group3
diff --git a/test/integration/targets/win_scheduled_task/tasks/main.yml b/test/integration/targets/win_scheduled_task/tasks/main.yml
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/win_scheduled_task/tasks/main.yml
@@ -0,0 +1,46 @@
+# NOTE: The win_scheduled_task module only works on Win2012+
+
+- name: Test Windows capabilities
+  raw: Get-Command New-ScheduledTask -ErrorAction SilentlyContinue; $?
+  failed_when: no
+  register: new_scheduledtask
+
+- name: Set boolean for capability
+  set_fact:
+    has_new_scheduledtask: '{{ new_scheduledtask.rc == 0 }}'
+
+- name: Test in normal mode
+  when: has_new_scheduledtask
+  block:
+  - include: tests.yml
+
+  - name: Check the various tasks in normal mode
+    assert:
+      that:
+      - add_scheduled_task.changed == true
+      - add_scheduled_task.exists == false
+      - add_scheduled_task_again.changed == false
+      - add_scheduled_task_again.exists == true
+      - remove_scheduled_task.changed == true
+      - remove_scheduled_task.exists == true
+      - remove_scheduled_task_again.changed == false
+      - remove_scheduled_task_again.exists == false
+
+
+- name: Test in check-mode
+  check_mode: yes
+  when: has_new_scheduledtask
+  block:
+  - include: tests.yml
+
+  - name: Check the various tests in check-mode
+    assert:
+      that:
+      - add_scheduled_task.changed == true
+      - add_scheduled_task.exists == false
+      - add_scheduled_task_again.changed == true
+      - add_scheduled_task_again.exists == false
+      - remove_scheduled_task.changed == false
+      - remove_scheduled_task.exists == false
+      - remove_scheduled_task_again.changed == false
+      - remove_scheduled_task_again.exists == false
diff --git a/test/integration/targets/win_scheduled_task/tasks/tests.yml b/test/integration/targets/win_scheduled_task/tasks/tests.yml
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/win_scheduled_task/tasks/tests.yml
@@ -0,0 +1,26 @@
+- name: Remove potentially leftover scheduled task
+  win_scheduled_task: &wst_absent
+    name: Test
+    state: absent
+
+- name: Add scheduled task
+  win_scheduled_task: &wst_present
+    name: Test
+    executable: dir.exe
+    arguments: C:\Windows\Temp\
+    frequency: once
+    time: 5pm
+    user: SYSTEM
+  register: add_scheduled_task
+
+- name: Add scheduled task (again)
+  win_scheduled_task: *wst_present
+  register: add_scheduled_task_again
+
+- name: Remove scheduled task
+  win_scheduled_task: *wst_absent
+  register: remove_scheduled_task
+
+- name: Remove scheduled task (again)
+  win_scheduled_task: *wst_absent
+  register: remove_scheduled_task_again