Code-review

Swap state enabled/disabled -> enabled yes/no
reviewable/pr18780/r1
Peter Mounce 10 years ago
parent d9211b709b
commit a4a3a13439

@ -33,33 +33,29 @@ else
{ {
Fail-Json $result "missing required argument: name" Fail-Json $result "missing required argument: name"
} }
if ($params.state) if ($params.enabled)
{ {
$state = $params.state.ToString() $enabled = $params.enabled | ConvertTo-Bool
if (($state -ne 'Enabled') -and ($state -ne 'Disabled'))
{
Fail-Json $result "state is '$state'; must be 'Enabled' or 'Disabled'"
}
} }
else else
{ {
$state = "Enabled" $enabled = $true
} }
$target_state = @{$true = "Enabled"; $false="Disabled"}[$enabled]
try try
{ {
$tasks = Get-ScheduledTask -TaskPath $name $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 (-not($tasks_needing_changing -eq $null))
{ {
if ($state -eq 'Disabled') if ($enabled)
{ {
$tasks_needing_changing | Disable-ScheduledTask $tasks_needing_changing | Enable-ScheduledTask
} }
elseif ($state -eq 'Enabled') else
{ {
$tasks_needing_changing | Enable-ScheduledTask $tasks_needing_changing | Disable-ScheduledTask
} }
Set-Attr $result "tasks_changed" ($tasks_needing_changing | foreach { $_.TaskPath + $_.TaskName }) Set-Attr $result "tasks_changed" ($tasks_needing_changing | foreach { $_.TaskPath + $_.TaskName })
$result.changed = $true $result.changed = $true

@ -34,18 +34,18 @@ options:
- Name of the scheduled task - Name of the scheduled task
- Supports * as wildcard - Supports * as wildcard
required: true required: true
state: enabled:
description: description:
- State that the task should become - State that the task should become
required: false required: false
choices: choices:
- Disabled - yes
- Enabled - no
default: Enabled default: yes
author: Peter Mounce author: Peter Mounce
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Disable the scheduled tasks with "WindowsUpdate" in their name # Disable the scheduled tasks with "WindowsUpdate" in their name
win_scheduled_task: name="*WindowsUpdate*" state=disabled win_scheduled_task: name="*WindowsUpdate*" enabled=no
''' '''

Loading…
Cancel
Save