Code-review

Swap state enabled/disabled -> enabled yes/no
This commit is contained in:
Peter Mounce 2015-05-19 11:21:23 +01:00 committed by Matt Clay
parent 8f74e4acdf
commit 08bf656309
2 changed files with 15 additions and 19 deletions

View file

@ -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
}

View file

@ -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
'''