Update method to determine if task exists, add days of week parameter to weekly triggers

reviewable/pr18780/r1
Michael Perzel 9 years ago
parent c12722f2ae
commit 3542e7d42d

@ -65,6 +65,9 @@ elseif ($state -eq "present")
{ {
Fail-Json $result "missing required argument: execute" 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) if ($params.path)
{ {
$path = "\{0}\" -f $params.path $path = "\{0}\" -f $params.path
@ -89,26 +92,40 @@ elseif($state -eq "present")
{ {
Fail-Json $result "missing required argument: time" Fail-Json $result "missing required argument: time"
} }
if ($params.daysOfWeek)
{
$daysOfWeek = $params.daysOfWeek
}
elseif ($frequency -eq "weekly")
{
Fail-Json $result "missing required argument: daysOfWeek"
}
$exists = $true
#hack to determine if task exists
try { try {
$task = Get-ScheduledTask -TaskName $name -TaskPath $path $task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"}
$measure = $task | measure
if ($measure.count -eq 1 ) {
$exists = $true
} }
catch { elseif ($measure.count -eq 0 -and $state -eq "absent" ){
$exists = $false | ConvertTo-Bool 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" Set-Attr $result "exists" "$exists"
try
{
if ($frequency){ if ($frequency){
if ($frequency -eq "daily") { if ($frequency -eq "daily") {
$trigger = New-ScheduledTaskTrigger -Daily -At $time $trigger = New-ScheduledTaskTrigger -Daily -At $time
} }
elseif (frequency -eq "weekly"){ elseif ($frequency -eq "weekly"){
$trigger = New-ScheduledTaskTrigger -Weekly -At $time $trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $daysOfWeek
} }
else { else {
Fail-Json $result "frequency must be daily or weekly" Fail-Json $result "frequency must be daily or weekly"

@ -57,6 +57,10 @@ options:
description: description:
- Time to execute scheduled task - Time to execute scheduled task
required: false required: false
daysOfWeek:
description:
- Days of the week to run a weekly task
required: false
path: path:
description: description:
- Folder path of scheduled task - Folder path of scheduled task

Loading…
Cancel
Save