Merge pull request #665 from perzizzle/scheduledTask

Ability to add/remove scheduled task
This commit is contained in:
Greg DeKoenigsberg 2015-09-30 16:11:45 -04:00
commit ff0a38ea65
2 changed files with 180 additions and 37 deletions

View file

@ -2,6 +2,7 @@
# This file is part of Ansible
#
# Copyright 2015, Peter Mounce <public@neverrunwithscissors.com>
# Michael Perzel <michaelperzel@gmail.com>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -25,48 +26,154 @@ $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
$user = Get-Attr -obj $params -name user -failifempty $true -resultobj $result
}
if ($params.days_of_week)
{
$days_of_week = $params.days_of_week
}
elseif ($frequency -eq "weekly")
{
Fail-Json $result "missing required argument: days_of_week"
}
# Vars with defaults
if ($params.enabled)
{
$enabled = $params.enabled | ConvertTo-Bool
}
else
{
$enabled = $true
$enabled = $true #default
}
$target_state = @{$true = "Enabled"; $false="Disabled"}[$enabled]
try
if ($params.description)
{
$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
$description = $params.description
}
else
{
$description = " " #default
}
if ($params.path)
{
$path = "\{0}\" -f $params.path
}
else
{
$path = "\" #default
}
# Optional vars
if ($params.argument)
{
$argument = $params.argument
}
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
{
$tasks_needing_changing | Disable-ScheduledTask
$taskState = $null
}
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;
$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"
if ($frequency){
if ($frequency -eq "daily") {
$trigger = New-ScheduledTaskTrigger -Daily -At $time
}
elseif ($frequency -eq "weekly"){
$trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $days_of_week
}
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
}
$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
}
else {
$action = New-ScheduledTaskAction -Execute $execute
}
if ( ($state -eq "present") -and ($exists -eq $false) ){
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 -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
Set-Attr $result "msg" "Updated task $name"
$result.changed = $true
}
}
Exit-Json $result;
}
catch
{

View file

@ -1,8 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2015, Peter Mounce <public@neverrunwithscissors.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
@ -32,20 +29,59 @@ options:
name:
description:
- Name of the scheduled task
- Supports * as wildcard
required: true
description:
description:
- The description for the scheduled task
required: false
enabled:
description:
- State that the task should become
required: false
- Enable/disable the task
choices:
- yes
- no
default: yes
author: Peter Mounce
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, not idempotent
required: false
choices:
- daily
- weekly
time:
description:
- Time to execute scheduled task, not idempotent
required: false
days_of_week:
description:
- Days of the week to run a weekly task, not idempotent
required: false
path:
description:
- Folder path of scheduled task
default: '\'
'''
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 user=SYSTEM
'''