From d1c896d31edf7bea35c02bd641555626b4caa79b Mon Sep 17 00:00:00 2001
From: Peter Mounce <peter.mounce@just-eat.com>
Date: Fri, 1 May 2015 21:17:34 +0100
Subject: [PATCH 1/6] win_scheduled_task module for windows

Fledgling module to allow scheduled tasks to be managed.
At present, I only need enabled/disabled support. There's lots of scope for more features.
---
 windows/win_scheduled_task.ps1 | 77 ++++++++++++++++++++++++++++++++++
 windows/win_scheduled_task.py  | 54 ++++++++++++++++++++++++
 2 files changed, 131 insertions(+)
 create mode 100644 windows/win_scheduled_task.ps1
 create mode 100644 windows/win_scheduled_task.py

diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1
new file mode 100644
index 00000000000..2716ed32ea9
--- /dev/null
+++ b/windows/win_scheduled_task.ps1
@@ -0,0 +1,77 @@
+#!powershell
+# This file is part of Ansible
+#
+# Copyright 2015, Peter Mounce <public@neverrunwithscissors.com>
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
+
+$ErrorActionPreference = "Stop"
+
+# WANT_JSON
+# POWERSHELL_COMMON
+
+$params = Parse-Args $args;
+$result = New-Object PSObject;
+Set-Attr $result "changed" $false;
+
+if ($params.name)
+{
+    $package = $params.name
+}
+else
+{
+    Fail-Json $result "missing required argument: name"
+}
+if ($params.state)
+{
+  $state = $params.state.ToString()
+  if (($state -ne 'Enabled') -and ($state -ne 'Disabled'))
+  {
+    Fail-Json $result "state is '$state'; must be 'Enabled' or 'Disabled'"
+  }
+}
+else
+{
+  $state = "Enabled"
+}
+
+
+try
+{
+  $tasks = Get-ScheduledTask -TaskPath $name
+  $tasks_needing_changing |? { $_.State -ne $state }
+  if ($tasks_needing_changing -eq $null)
+  {
+    if ($state -eq 'Disabled')
+    {
+      $tasks_needing_changing | Disable-ScheduledTask
+    }
+    elseif ($state -eq 'Enabled')
+    {
+      $tasks_needing_changing | Enable-ScheduledTask
+    }
+    Set-Attr $result "tasks_changed" ($tasks_needing_changing | foreach { $_.TaskPath + $_.TaskName })
+    $result.changed = $true
+  }
+  else
+  {
+    Set-Attr $result "tasks_changed" @()
+    $result.changed = $false
+  }
+  Exit-Json $result;
+}
+catch
+{
+  Fail-Json $result $_.Exception.Message
+}
diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py
new file mode 100644
index 00000000000..ac353c14c0a
--- /dev/null
+++ b/windows/win_scheduled_task.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# (c) 2015, Peter Mounce <public@neverrunwithscissors.com>
+#
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
+
+# this is a windows documentation stub.  actual code lives in the .ps1
+# file of the same name
+
+DOCUMENTATION = '''
+---
+module: win_scheduled_task
+version_added: "1.9"
+short_description: Manage scheduled tasks
+description:
+    - Manage scheduled tasks
+options:
+  name:
+    description:
+      - Name of the scheduled task
+      - Supports * as wildcard
+    required: true
+    default: null
+    aliases: []
+  state:
+    description:
+      - State that the task should become
+    required: false
+    choices:
+      - Disabled
+      - Enabled
+    default: Enabled
+    aliases: []
+author: Peter Mounce
+'''
+
+EXAMPLES = '''
+  # Disable the scheduled tasks with "WindowsUpdate" in their name
+  win_scheduled_task: name="*WindowsUpdate*" state=disabled
+'''

From 6f1d9fbbccea3c37f3ab672a544903297da311a5 Mon Sep 17 00:00:00 2001
From: Peter Mounce <peter.mounce@just-eat.com>
Date: Sat, 2 May 2015 13:56:01 +0100
Subject: [PATCH 2/6] correct variable name

---
 windows/win_scheduled_task.ps1 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1
index 2716ed32ea9..763bfb53862 100644
--- a/windows/win_scheduled_task.ps1
+++ b/windows/win_scheduled_task.ps1
@@ -27,7 +27,7 @@ Set-Attr $result "changed" $false;
 
 if ($params.name)
 {
-    $package = $params.name
+    $name = $params.name
 }
 else
 {

From 4fef779f09b0d3b8d7fd7fa893d54c4fc09f2475 Mon Sep 17 00:00:00 2001
From: Peter Mounce <peter.mounce@just-eat.com>
Date: Sat, 2 May 2015 17:24:30 +0100
Subject: [PATCH 3/6] caught out by syntax

---
 windows/win_scheduled_task.ps1 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1
index 763bfb53862..52b68dd5b6a 100644
--- a/windows/win_scheduled_task.ps1
+++ b/windows/win_scheduled_task.ps1
@@ -50,8 +50,8 @@ else
 try
 {
   $tasks = Get-ScheduledTask -TaskPath $name
-  $tasks_needing_changing |? { $_.State -ne $state }
-  if ($tasks_needing_changing -eq $null)
+  $tasks_needing_changing = $tasks |? { $_.State -ne $state }
+  if (-not($tasks_needing_changing -eq $null))
   {
     if ($state -eq 'Disabled')
     {
@@ -69,6 +69,7 @@ try
     Set-Attr $result "tasks_changed" @()
     $result.changed = $false
   }
+
   Exit-Json $result;
 }
 catch

From ede4820562423632610359c07623a158acf0282f Mon Sep 17 00:00:00 2001
From: Peter Mounce <peter.mounce@just-eat.com>
Date: Wed, 6 May 2015 21:47:39 +0100
Subject: [PATCH 4/6] version_added -> 2, remove empty aliases

---
 windows/win_scheduled_task.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py
index ac353c14c0a..e755890b319 100644
--- a/windows/win_scheduled_task.py
+++ b/windows/win_scheduled_task.py
@@ -24,7 +24,7 @@
 DOCUMENTATION = '''
 ---
 module: win_scheduled_task
-version_added: "1.9"
+version_added: "2.0"
 short_description: Manage scheduled tasks
 description:
     - Manage scheduled tasks
@@ -35,7 +35,6 @@ options:
       - Supports * as wildcard
     required: true
     default: null
-    aliases: []
   state:
     description:
       - State that the task should become
@@ -44,7 +43,6 @@ options:
       - Disabled
       - Enabled
     default: Enabled
-    aliases: []
 author: Peter Mounce
 '''
 

From d9211b709b2f6a8bb46118ec3ae95907551c158f Mon Sep 17 00:00:00 2001
From: Peter Mounce <peter.mounce@just-eat.com>
Date: Wed, 6 May 2015 21:48:19 +0100
Subject: [PATCH 5/6] no default, remove it

---
 windows/win_scheduled_task.py | 1 -
 1 file changed, 1 deletion(-)

diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py
index e755890b319..7c604ecec20 100644
--- a/windows/win_scheduled_task.py
+++ b/windows/win_scheduled_task.py
@@ -34,7 +34,6 @@ options:
       - Name of the scheduled task
       - Supports * as wildcard
     required: true
-    default: null
   state:
     description:
       - State that the task should become

From a4a3a1343953cf996b57bb6b91a55cdb6678ca12 Mon Sep 17 00:00:00 2001
From: Peter Mounce <peter.mounce@just-eat.com>
Date: Tue, 19 May 2015 11:21:23 +0100
Subject: [PATCH 6/6] Code-review

Swap state enabled/disabled -> enabled yes/no
---
 windows/win_scheduled_task.ps1 | 24 ++++++++++--------------
 windows/win_scheduled_task.py  | 10 +++++-----
 2 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1
index 52b68dd5b6a..2f802f59cd0 100644
--- a/windows/win_scheduled_task.ps1
+++ b/windows/win_scheduled_task.ps1
@@ -33,34 +33,30 @@ else
 {
     Fail-Json $result "missing required argument: name"
 }
-if ($params.state)
+if ($params.enabled)
 {
-  $state = $params.state.ToString()
-  if (($state -ne 'Enabled') -and ($state -ne 'Disabled'))
-  {
-    Fail-Json $result "state is '$state'; must be 'Enabled' or 'Disabled'"
-  }
+  $enabled = $params.enabled | ConvertTo-Bool
 }
 else
 {
-  $state = "Enabled"
+  $enabled = $true
 }
-
+$target_state = @{$true = "Enabled"; $false="Disabled"}[$enabled]
 
 try
 {
   $tasks = Get-ScheduledTask -TaskPath $name
-  $tasks_needing_changing = $tasks |? { $_.State -ne $state }
+  $tasks_needing_changing = $tasks |? { $_.State -ne $target_state }
   if (-not($tasks_needing_changing -eq $null))
   {
-    if ($state -eq 'Disabled')
-    {
-      $tasks_needing_changing | Disable-ScheduledTask
-    }
-    elseif ($state -eq 'Enabled')
+    if ($enabled)
     {
       $tasks_needing_changing | Enable-ScheduledTask
     }
+    else
+    {
+      $tasks_needing_changing | Disable-ScheduledTask
+    }
     Set-Attr $result "tasks_changed" ($tasks_needing_changing | foreach { $_.TaskPath + $_.TaskName })
     $result.changed = $true
   }
diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py
index 7c604ecec20..2c5867402c5 100644
--- a/windows/win_scheduled_task.py
+++ b/windows/win_scheduled_task.py
@@ -34,18 +34,18 @@ options:
       - Name of the scheduled task
       - Supports * as wildcard
     required: true
-  state:
+  enabled:
     description:
       - State that the task should become
     required: false
     choices:
-      - Disabled
-      - Enabled
-    default: Enabled
+      - yes
+      - no
+    default: yes
 author: Peter Mounce
 '''
 
 EXAMPLES = '''
   # Disable the scheduled tasks with "WindowsUpdate" in their name
-  win_scheduled_task: name="*WindowsUpdate*" state=disabled
+  win_scheduled_task: name="*WindowsUpdate*" enabled=no
 '''