Code-review
Swap state enabled/disabled -> enabled yes/no
This commit is contained in:
parent
8f74e4acdf
commit
08bf656309
2 changed files with 15 additions and 19 deletions
|
@ -33,34 +33,30 @@ 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
|
|
||||||
}
|
|
||||||
elseif ($state -eq 'Enabled')
|
|
||||||
{
|
{
|
||||||
$tasks_needing_changing | Enable-ScheduledTask
|
$tasks_needing_changing | Enable-ScheduledTask
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$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…
Reference in a new issue