Allow win_scheduled_task to support adding and removing task paths (#24025)

* allows win_scheduled_task to support adding and removing task paths

* fix line length for documentation

* added integration tests for path creation and removal

* removing ability to remove TaskPath if a task isn't removed.  also removed superfluous line of code in Invoke-TaskPathCheck function
This commit is contained in:
Andrew Saraceni 2017-06-14 03:02:36 -04:00 committed by jhawkesworth
commit 91e995d691
3 changed files with 161 additions and 5 deletions

View file

@ -26,13 +26,55 @@ $result = @{
changed = $false
}
function Invoke-TaskPathCheck {
[CmdletBinding(SupportsShouldProcess=$true)]
param($Path, [Switch]$Remove)
$pathResults = @{
PathExists = $null
RemovedPath = $false
}
if ($path -ne "\") {
$trimmedPath = $Path.TrimEnd("\")
}
else {
$trimmedPath = $Path
}
$scheduleObject = New-Object -ComObject Schedule.Service
$scheduleObject.Connect()
try {
$targetFolder = $scheduleObject.GetFolder($trimmedPath)
$pathResults.PathExists = $true
}
catch {
$pathResults.PathExists = $false
}
if ($Remove -and $pathResults.PathExists) {
$childFolders = $targetFolder.GetFolders($null)
$childTasks = $targetFolder.GetTasks($null)
if ($childFolders.Count -eq 0 -and $childTasks.Count -eq 0) {
if ($PSCmdlet.ShouldProcess($trimmedPath, "Remove task path")) {
$rootFolder = $scheduleObject.GetFolder("\")
$rootFolder.DeleteFolder($trimmedPath, $null)
}
$pathResults.RemovedPath = $true
}
}
return $pathResults
}
$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$arguments = Get-AnsibleParam -obj $params -name "arguments" -type "str" -aliases "argument"
$description = Get-AnsibleParam -obj $params -name "description" -type "str" -default "No description."
$enabled = Get-AnsibleParam -obj $params -name "enabled" -type "bool" -default $true
# TODO: We do not create the TaskPath if missing
$path = Get-AnsibleParam -obj $params -name "path" -type "str" -default '\'
# Required vars
@ -53,7 +95,7 @@ $days_of_week = Get-AnsibleParam -obj $params -name "days_of_week" -type "str" -
try {
$task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"}
$task = Get-ScheduledTask | Where-Object {$_.TaskName -eq $name -and $_.TaskPath -eq $path}
# Correlate task state to enable variable, used to calculate if state needs to be changed
$taskState = if ($task) { $task.State } else { $null }
@ -76,6 +118,7 @@ try {
# Nothing to do
$result.exists = $false
$result.msg = "Task does not exist"
Exit-Json $result
}
elseif ($measure.count -eq 0){
@ -107,6 +150,14 @@ try {
Unregister-ScheduledTask -TaskName $name -Confirm:$false -WhatIf:$check_mode
$result.changed = $true
$result.msg = "Deleted task $name"
# Remove task path if it exists
$pathResults = Invoke-TaskPathCheck -Path $path -Remove -WhatIf:$check_mode
if ($pathResults.RemovedPath) {
$result.msg += " and task path $path removed"
}
Exit-Json $result
}
elseif ( ($state -eq "absent") -and (-not $exists) ) {
@ -131,25 +182,41 @@ try {
}
if ( ($state -eq "present") -and (-not $exists) ){
# Check task path prior to registering
$pathResults = Invoke-TaskPathCheck -Path $path
if (-not $check_mode) {
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal
# $task = Get-ScheduledTask -TaskName $name
}
$result.changed = $true
$result.msg = "Added new task $name"
if (!$pathResults.PathExists) {
$result.msg += " and task path $path created"
}
}
elseif( ($state -eq "present") -and ($exists) ) {
# Check task path prior to registering
$pathResults = Invoke-TaskPathCheck -Path $path
if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $executable -and $taskState -eq $enabled -and $task.Principal.UserId -eq $user) {
# No change in the task
$result.msg = "No change in task $name"
}
else {
Unregister-ScheduledTask -TaskName $name -Confirm:$false -WhatIf:$check_mode
if (-not $check_mode) {
$oldPathResults = Invoke-TaskPathCheck -Path $task.TaskPath -Remove
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal
}
$result.changed = $true
$result.msg = "Updated task $name"
if (!$pathResults.PathExists) {
$result.msg += " and task path $path created"
}
}
}