From d6339c47e4b52f64e310ca1540432bd7966f20a8 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 30 Jun 2015 12:06:16 -0500 Subject: [PATCH 01/13] Ability to add/remove scheduled task --- windows/win_scheduled_task.ps1 | 137 +++++++++++++++++++++++++++------ windows/win_scheduled_task.py | 38 +++++++-- 2 files changed, 145 insertions(+), 30 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index 2f802f59cd0..07b1c3adf60 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -33,42 +33,135 @@ else { Fail-Json $result "missing required argument: name" } +if ($params.state) +{ + $state = $params.state +} +else +{ + Fail-Json $result "missing required argument: state" +} if ($params.enabled) { $enabled = $params.enabled | ConvertTo-Bool } else { - $enabled = $true + $enabled = $true #default +} +if ($params.description) +{ + $description = $params.description +} +else +{ + $description = " " #default +} +if ($params.execute) +{ + $execute = $params.execute +} +elseif ($state -eq "present") +{ + Fail-Json $result "missing required argument: execute" +} +if ($params.path) +{ + $path = "\{0}\" -f $params.path +} +else +{ + $path = "\" #default +} +if ($params.frequency) +{ + $frequency = $params.frequency +} +elseif($state -eq "present") +{ + Fail-Json $result "missing required argument: frequency" +} +if ($params.time) +{ + $time = $params.time +} +elseif($state -eq "present") +{ + Fail-Json $result "missing required argument: time" +} + +$exists = $true +#hack to determine if task exists +try { + $task = Get-ScheduledTask -TaskName $name -TaskPath $path +} +catch { + $exists = $false | ConvertTo-Bool } -$target_state = @{$true = "Enabled"; $false="Disabled"}[$enabled] +Set-Attr $result "exists" "$exists" + try { - $tasks = Get-ScheduledTask -TaskPath $name - $tasks_needing_changing = $tasks |? { $_.State -ne $target_state } - if (-not($tasks_needing_changing -eq $null)) - { - if ($enabled) - { - $tasks_needing_changing | Enable-ScheduledTask + if ($frequency){ + if ($frequency -eq "daily") { + $trigger = New-ScheduledTaskTrigger -Daily -At $time + } + elseif (frequency -eq "weekly"){ + $trigger = New-ScheduledTaskTrigger -Weekly -At $time + } + else { + Fail-Json $result "frequency must be daily or weekly" + } + } + + if ($state -eq "absent" -and $exists -eq $true) { + Unregister-ScheduledTask -TaskName $name -Confirm:$false + $result.changed = $true + Set-Attr $result "msg" "Deleted task $name" + Exit-Json $result + } + elseif ($state -eq "absent" -and $exists -eq $false) { + Set-Attr $result "msg" "Task $name does not exist" + Exit-Json $result + } + + if ($state -eq "present" -and $exists -eq $false){ + $action = New-ScheduledTaskAction -Execute $execute + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path + $task = Get-ScheduledTask -TaskName $name + Set-Attr $result "msg" "Added new task $name" + $result.changed = $true } - else - { - $tasks_needing_changing | Disable-ScheduledTask + elseif($state -eq "present" -and $exists -eq $true) { + if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute) { + #No change in the task yet + Set-Attr $result "msg" "No change in task $name" + } + else { + Unregister-ScheduledTask -TaskName $name -Confirm:$false + $action = New-ScheduledTaskAction -Execute $execute + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path + $task = Get-ScheduledTask -TaskName $name + Set-Attr $result "msg" "Updated task $name" + $result.changed = $true + } } - 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; + if ($state -eq "present" -and $enabled -eq $true -and $task.State -ne "Ready" ){ + $task | Enable-ScheduledTask + Set-Attr $result "msg" "Enabled task $name" + $result.changed = $true + } + elseif ($state -eq "present" -and $enabled -eq $false -and $task.State -ne "Disabled"){ + $task | Disable-ScheduledTask + Set-Attr $result "msg" "Disabled task $name" + $result.changed = $true + } + + Exit-Json $result; } catch { Fail-Json $result $_.Exception.Message -} +} \ No newline at end of file diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py index 2c5867402c5..4ab830d05e1 100644 --- a/windows/win_scheduled_task.py +++ b/windows/win_scheduled_task.py @@ -1,8 +1,5 @@ #!/usr/bin/python # -*- coding: utf-8 -*- - -# (c) 2015, Peter Mounce -# # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify @@ -32,20 +29,45 @@ options: name: description: - Name of the scheduled task - - Supports * as wildcard required: true enabled: description: - - State that the task should become + - Enable/disable the task required: false choices: - yes - no default: yes -author: Peter Mounce + state: + description: + - State that the task should become + required: false + choices: + - present + - absent + execute: + description: + - Command the scheduled task should execute + required: false + frequency: + description: + - The frequency of the command + choices: + - daily + - weekly + required: false + time: + description: + - Time to execute scheduled task + required: false + path: + description: + - Folder path of scheduled task + default: '\' + required: false ''' EXAMPLES = ''' - # Disable the scheduled tasks with "WindowsUpdate" in their name - win_scheduled_task: name="*WindowsUpdate*" enabled=no + # Create a scheduled task to open a command prompt + win_scheduled_task: name="TaskName" execute="cmd" frequency="daily" time="9am" description="open command prompt" path="example" enable=yes state=present ''' From 00d3a629076cef00917101d0251662f00cf0842d Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 30 Jun 2015 12:24:00 -0500 Subject: [PATCH 02/13] Documentation updates --- windows/win_scheduled_task.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py index 4ab830d05e1..9f73cc30612 100644 --- a/windows/win_scheduled_task.py +++ b/windows/win_scheduled_task.py @@ -33,7 +33,6 @@ options: enabled: description: - Enable/disable the task - required: false choices: - yes - no @@ -41,7 +40,6 @@ options: state: description: - State that the task should become - required: false choices: - present - absent @@ -55,7 +53,6 @@ options: choices: - daily - weekly - required: false time: description: - Time to execute scheduled task @@ -64,7 +61,6 @@ options: description: - Folder path of scheduled task default: '\' - required: false ''' EXAMPLES = ''' From 3542e7d42dcb7c598ed5f1a515ac6a1718f27b2f Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 7 Jul 2015 11:55:46 -0500 Subject: [PATCH 03/13] Update method to determine if task exists, add days of week parameter to weekly triggers --- windows/win_scheduled_task.ps1 | 43 ++++++++++++++++++++++++---------- windows/win_scheduled_task.py | 4 ++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index 07b1c3adf60..d5102572e69 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -65,6 +65,9 @@ elseif ($state -eq "present") { Fail-Json $result "missing required argument: execute" } +if( $state -ne "present" -and $state -ne "absent") { + Fail-Json $result "state must be present or absent" +} if ($params.path) { $path = "\{0}\" -f $params.path @@ -89,26 +92,40 @@ elseif($state -eq "present") { Fail-Json $result "missing required argument: time" } - -$exists = $true -#hack to determine if task exists -try { - $task = Get-ScheduledTask -TaskName $name -TaskPath $path +if ($params.daysOfWeek) +{ + $daysOfWeek = $params.daysOfWeek } -catch { - $exists = $false | ConvertTo-Bool +elseif ($frequency -eq "weekly") +{ + Fail-Json $result "missing required argument: daysOfWeek" } -Set-Attr $result "exists" "$exists" +try { + $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"} + $measure = $task | measure + if ($measure.count -eq 1 ) { + $exists = $true + } + elseif ($measure.count -eq 0 -and $state -eq "absent" ){ + Set-Attr $result "msg" "Task does not exist" + Exit-Json $result + } + elseif ($measure.count -eq 0){ + $exists = $false + } + else { + # This should never occur + Fail-Json $result "$measure.count scheduled tasks found" + } + Set-Attr $result "exists" "$exists" -try -{ if ($frequency){ if ($frequency -eq "daily") { $trigger = New-ScheduledTaskTrigger -Daily -At $time } - elseif (frequency -eq "weekly"){ - $trigger = New-ScheduledTaskTrigger -Weekly -At $time + elseif ($frequency -eq "weekly"){ + $trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $daysOfWeek } else { Fail-Json $result "frequency must be daily or weekly" @@ -164,4 +181,4 @@ try catch { Fail-Json $result $_.Exception.Message -} \ No newline at end of file +} diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py index 9f73cc30612..8b078ae9ae8 100644 --- a/windows/win_scheduled_task.py +++ b/windows/win_scheduled_task.py @@ -57,6 +57,10 @@ options: description: - Time to execute scheduled task required: false + daysOfWeek: + description: + - Days of the week to run a weekly task + required: false path: description: - Folder path of scheduled task From cdb36ea0fc022e7ee91c36459463337864576dfc Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 21 Jul 2015 17:35:33 -0500 Subject: [PATCH 04/13] Cleanup enable/disable logic --- windows/win_scheduled_task.ps1 | 43 +++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index d5102572e69..e09544226d5 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -2,6 +2,7 @@ # This file is part of Ansible # # Copyright 2015, Peter Mounce +# Michael Perzel # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -103,6 +104,20 @@ elseif ($frequency -eq "weekly") try { $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"} + + # Correlate task state to enable variable, used to calculate if state needs to be changed + $taskState = $task.State + if ($taskState -eq "Ready"){ + $taskState = $true + } + elseif($taskState -eq "Disabled"){ + $taskState = $false + } + else + { + $taskState = $null + } + $measure = $task | measure if ($measure.count -eq 1 ) { $exists = $true @@ -118,6 +133,7 @@ try { # This should never occur Fail-Json $result "$measure.count scheduled tasks found" } + Set-Attr $result "exists" "$exists" if ($frequency){ @@ -143,39 +159,38 @@ try { Exit-Json $result } + if ($enabled -eq $false){ + $settings = New-ScheduledTaskSettingsSet -Disable + } + else { + $settings = New-ScheduledTaskSettingsSet + } + if ($state -eq "present" -and $exists -eq $false){ $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $ +description -TaskPath $path -Settings $settings $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Added new task $name" $result.changed = $true } elseif($state -eq "present" -and $exists -eq $true) { - if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute) { + if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPat +h -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled) { #No change in the task yet Set-Attr $result "msg" "No change in task $name" } else { Unregister-ScheduledTask -TaskName $name -Confirm:$false $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Descripti +on $description -TaskPath $path -Settings $settings $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Updated task $name" $result.changed = $true } } - if ($state -eq "present" -and $enabled -eq $true -and $task.State -ne "Ready" ){ - $task | Enable-ScheduledTask - Set-Attr $result "msg" "Enabled task $name" - $result.changed = $true - } - elseif ($state -eq "present" -and $enabled -eq $false -and $task.State -ne "Disabled"){ - $task | Disable-ScheduledTask - Set-Attr $result "msg" "Disabled task $name" - $result.changed = $true - } - Exit-Json $result; } catch From 681cdd1c12dd434a3bf7cb43e33f47d0cd039726 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 21 Jul 2015 17:38:27 -0500 Subject: [PATCH 05/13] Remove accidental newlines --- windows/win_scheduled_task.ps1 | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index e09544226d5..c54be17db46 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -168,23 +168,20 @@ try { if ($state -eq "present" -and $exists -eq $false){ $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $ -description -TaskPath $path -Settings $settings + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Added new task $name" $result.changed = $true } elseif($state -eq "present" -and $exists -eq $true) { - if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPat -h -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled) { + if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled) { #No change in the task yet Set-Attr $result "msg" "No change in task $name" } else { Unregister-ScheduledTask -TaskName $name -Confirm:$false $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Descripti -on $description -TaskPath $path -Settings $settings + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Updated task $name" $result.changed = $true From a9eb69b0fa54d8299fddae23f6c89d4d0ebf7a6a Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 11:18:26 -0500 Subject: [PATCH 06/13] Use helper methods to validate vars. Cleanup logic. --- windows/win_scheduled_task.ps1 | 62 ++++++++++------------------------ 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index c54be17db46..85b54c89d05 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -26,22 +26,29 @@ $params = Parse-Args $args; $result = New-Object PSObject; Set-Attr $result "changed" $false; -if ($params.name) -{ - $name = $params.name +#Required vars +$name = Get-Attr -obj $params -name name -failifempty $true -resultobj $result +$state = Get-Attr -obj $params -name state -failifempty $true -resultobj $result +if( ($state -ne "present") -and ($state -ne "absent") ) { + Fail-Json $result "state must be present or absent" } -else -{ - Fail-Json $result "missing required argument: name" + +#Vars conditionally required +if($state -eq "present") { + $execute = Get-Attr -obj $params -name execute -failifempty $true -resultobj $result + $frequency = Get-Attr -obj $params -name frequency -failifempty $true -resultobj $result + $time = Get-Attr -obj $params -name time -failifempty $true -resultobj $result } -if ($params.state) +if ($params.daysOfWeek) { - $state = $params.state + $daysOfWeek = $params.daysOfWeek } -else +elseif ($frequency -eq "weekly") { - Fail-Json $result "missing required argument: state" + Fail-Json $result "missing required argument: daysOfWeek" } + +# Vars with defaults if ($params.enabled) { $enabled = $params.enabled | ConvertTo-Bool @@ -58,17 +65,6 @@ else { $description = " " #default } -if ($params.execute) -{ - $execute = $params.execute -} -elseif ($state -eq "present") -{ - Fail-Json $result "missing required argument: execute" -} -if( $state -ne "present" -and $state -ne "absent") { - Fail-Json $result "state must be present or absent" -} if ($params.path) { $path = "\{0}\" -f $params.path @@ -77,30 +73,6 @@ else { $path = "\" #default } -if ($params.frequency) -{ - $frequency = $params.frequency -} -elseif($state -eq "present") -{ - Fail-Json $result "missing required argument: frequency" -} -if ($params.time) -{ - $time = $params.time -} -elseif($state -eq "present") -{ - Fail-Json $result "missing required argument: time" -} -if ($params.daysOfWeek) -{ - $daysOfWeek = $params.daysOfWeek -} -elseif ($frequency -eq "weekly") -{ - Fail-Json $result "missing required argument: daysOfWeek" -} try { $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"} From d0b4bc0dda729bb87a49ac1a7ccf281aa5a6c6d5 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 11:20:35 -0500 Subject: [PATCH 07/13] Show order of operations with parenthesis --- windows/win_scheduled_task.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index 85b54c89d05..5777d198cc8 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -94,7 +94,7 @@ try { if ($measure.count -eq 1 ) { $exists = $true } - elseif ($measure.count -eq 0 -and $state -eq "absent" ){ + elseif ( ($measure.count -eq 0) -and ($state -eq "absent") ){ Set-Attr $result "msg" "Task does not exist" Exit-Json $result } @@ -120,13 +120,13 @@ try { } } - if ($state -eq "absent" -and $exists -eq $true) { + if ( ($state -eq "absent") -and ($exists -eq $true) ) { Unregister-ScheduledTask -TaskName $name -Confirm:$false $result.changed = $true Set-Attr $result "msg" "Deleted task $name" Exit-Json $result } - elseif ($state -eq "absent" -and $exists -eq $false) { + elseif ( ($state -eq "absent") -and ($exists -eq $false) ) { Set-Attr $result "msg" "Task $name does not exist" Exit-Json $result } @@ -138,14 +138,14 @@ try { $settings = New-ScheduledTaskSettingsSet } - if ($state -eq "present" -and $exists -eq $false){ + if ( ($state -eq "present") -and ($exists -eq $false) ){ $action = New-ScheduledTaskAction -Execute $execute Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Added new task $name" $result.changed = $true } - elseif($state -eq "present" -and $exists -eq $true) { + elseif( ($state -eq "present") -and ($exists -eq $true) ) { if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled) { #No change in the task yet Set-Attr $result "msg" "No change in task $name" From 027dff6d3d279ec1eaafbd64ac2cdda0eb1fe75b Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 13:37:39 -0500 Subject: [PATCH 08/13] Add support for command arguments --- windows/win_scheduled_task.ps1 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index 5777d198cc8..4b64b7999e2 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -74,6 +74,12 @@ else $path = "\" #default } +# Optional vars +if ($params.argument) +{ + $argument = $params.argument +} + try { $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"} @@ -137,6 +143,13 @@ try { else { $settings = New-ScheduledTaskSettingsSet } + + if ($argument) { + $action = New-ScheduledTaskAction -Execute $execute -Argument $argument + } + else { + $action = New-ScheduledTaskAction -Execute $execute + } if ( ($state -eq "present") -and ($exists -eq $false) ){ $action = New-ScheduledTaskAction -Execute $execute @@ -152,9 +165,7 @@ try { } else { Unregister-ScheduledTask -TaskName $name -Confirm:$false - $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings - $task = Get-ScheduledTask -TaskName $name + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal Set-Attr $result "msg" "Updated task $name" $result.changed = $true } From de1696cb74804c80b095620f9a87c04e3f37b460 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 14:16:30 -0500 Subject: [PATCH 09/13] Add support for specifying user for scheduled task to run as --- windows/win_scheduled_task.ps1 | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index 4b64b7999e2..e71b9bd7b7e 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -38,14 +38,15 @@ if($state -eq "present") { $execute = Get-Attr -obj $params -name execute -failifempty $true -resultobj $result $frequency = Get-Attr -obj $params -name frequency -failifempty $true -resultobj $result $time = Get-Attr -obj $params -name time -failifempty $true -resultobj $result + $user = Get-Attr -obj $params -name user -failifempty $true -resultobj $result } -if ($params.daysOfWeek) +if ($params.days_of_week) { - $daysOfWeek = $params.daysOfWeek + $days_of_week = $params.days_of_week } elseif ($frequency -eq "weekly") { - Fail-Json $result "missing required argument: daysOfWeek" + Fail-Json $result "missing required argument: days_of_week" } # Vars with defaults @@ -119,7 +120,7 @@ try { $trigger = New-ScheduledTaskTrigger -Daily -At $time } elseif ($frequency -eq "weekly"){ - $trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $daysOfWeek + $trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $days_of_week } else { Fail-Json $result "frequency must be daily or weekly" @@ -137,13 +138,15 @@ try { Exit-Json $result } + $principal = New-ScheduledTaskPrincipal -UserId "$user" -LogonType ServiceAccount + if ($enabled -eq $false){ $settings = New-ScheduledTaskSettingsSet -Disable } else { $settings = New-ScheduledTaskSettingsSet } - + if ($argument) { $action = New-ScheduledTaskAction -Execute $execute -Argument $argument } @@ -153,19 +156,19 @@ try { if ( ($state -eq "present") -and ($exists -eq $false) ){ $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Added new task $name" $result.changed = $true } elseif( ($state -eq "present") -and ($exists -eq $true) ) { - if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled) { - #No change in the task yet + if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled -and $task.Principal.UserId -eq $user) { + #No change in the task Set-Attr $result "msg" "No change in task $name" } else { Unregister-ScheduledTask -TaskName $name -Confirm:$false - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal + Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal Set-Attr $result "msg" "Updated task $name" $result.changed = $true } @@ -176,4 +179,4 @@ try { catch { Fail-Json $result $_.Exception.Message -} +} \ No newline at end of file From 64416ff094382b9d72ac01f81664633b287c2622 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 14:35:34 -0500 Subject: [PATCH 10/13] Fix logging of error message --- 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 e71b9bd7b7e..e6effebb7bd 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -110,7 +110,7 @@ try { } else { # This should never occur - Fail-Json $result "$measure.count scheduled tasks found" + Fail-Json $result "$($measure.count) scheduled tasks found" } Set-Attr $result "exists" "$exists" From 685c935a371c6ae64c870585f4caaa6aa4f5092d Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 14:36:11 -0500 Subject: [PATCH 11/13] Documentation updates --- windows/win_scheduled_task.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py index 8b078ae9ae8..bb2cdbc0b82 100644 --- a/windows/win_scheduled_task.py +++ b/windows/win_scheduled_task.py @@ -30,6 +30,10 @@ options: description: - Name of the scheduled task required: true + description: + description: + - The description for the scheduled task + required: false enabled: description: - Enable/disable the task @@ -40,16 +44,26 @@ options: state: description: - State that the task should become + required: true choices: - present - absent + user: + description: + - User to run scheduled task as + required: false execute: description: - Command the scheduled task should execute required: false + argument: + description: + - Arguments to provide scheduled task action + required: false frequency: description: - The frequency of the command + required: false choices: - daily - weekly @@ -57,7 +71,7 @@ options: description: - Time to execute scheduled task required: false - daysOfWeek: + days_of_week: description: - Days of the week to run a weekly task required: false @@ -69,5 +83,5 @@ options: EXAMPLES = ''' # Create a scheduled task to open a command prompt - win_scheduled_task: name="TaskName" execute="cmd" frequency="daily" time="9am" description="open command prompt" path="example" enable=yes state=present + win_scheduled_task: name="TaskName" execute="cmd" frequency="daily" time="9am" description="open command prompt" path="example" enable=yes state=present user=SYSTEM ''' From a7675e662154f4475202a37049821777d04c598e Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 14:40:21 -0500 Subject: [PATCH 12/13] Note parameters that are not idempotent --- windows/win_scheduled_task.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/windows/win_scheduled_task.py b/windows/win_scheduled_task.py index bb2cdbc0b82..e26cbc00cf0 100644 --- a/windows/win_scheduled_task.py +++ b/windows/win_scheduled_task.py @@ -62,18 +62,18 @@ options: required: false frequency: description: - - The frequency of the command + - The frequency of the command, not idempotent required: false choices: - daily - weekly time: description: - - Time to execute scheduled task + - Time to execute scheduled task, not idempotent required: false days_of_week: description: - - Days of the week to run a weekly task + - Days of the week to run a weekly task, not idempotent required: false path: description: From 123a2b25ed35fec0fd9028f27749df945313d838 Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 14:45:21 -0500 Subject: [PATCH 13/13] Remove duplicate action declaration. --- windows/win_scheduled_task.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/windows/win_scheduled_task.ps1 b/windows/win_scheduled_task.ps1 index e6effebb7bd..b63bd130134 100644 --- a/windows/win_scheduled_task.ps1 +++ b/windows/win_scheduled_task.ps1 @@ -155,7 +155,6 @@ try { } if ( ($state -eq "present") -and ($exists -eq $false) ){ - $action = New-ScheduledTaskAction -Execute $execute Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal $task = Get-ScheduledTask -TaskName $name Set-Attr $result "msg" "Added new task $name"