From 0f9c81a8c28c07a5b771d3311b59dd97ec37eddd Mon Sep 17 00:00:00 2001 From: Michael Perzel Date: Tue, 8 Sep 2015 14:16:30 -0500 Subject: [PATCH] Add support for specifying user for scheduled task to run as --- .../extras/windows/win_scheduled_task.ps1 | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 b/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 index 4b64b7999e..e71b9bd7b7 100644 --- a/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 +++ b/lib/ansible/modules/extras/windows/win_scheduled_task.ps1 @@ -38,14 +38,15 @@ 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.daysOfWeek) +if ($params.days_of_week) { - $daysOfWeek = $params.daysOfWeek + $days_of_week = $params.days_of_week } elseif ($frequency -eq "weekly") { - Fail-Json $result "missing required argument: daysOfWeek" + Fail-Json $result "missing required argument: days_of_week" } # Vars with defaults @@ -119,7 +120,7 @@ try { $trigger = New-ScheduledTaskTrigger -Daily -At $time } elseif ($frequency -eq "weekly"){ - $trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $daysOfWeek + $trigger = New-ScheduledTaskTrigger -Weekly -At $time -DaysOfWeek $days_of_week } else { Fail-Json $result "frequency must be daily or weekly" @@ -137,13 +138,15 @@ try { 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 } @@ -153,19 +156,19 @@ try { if ( ($state -eq "present") -and ($exists -eq $false) ){ $action = New-ScheduledTaskAction -Execute $execute - Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings + 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) { - #No change in the task yet + 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 + 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 } @@ -176,4 +179,4 @@ try { catch { Fail-Json $result $_.Exception.Message -} +} \ No newline at end of file