Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduled Job through PowerShell quits after 3 days

I have a script that can legitimately run much longer than 3 days (it's a command queuing script, so it has a lot of pauses in it waiting for a job to complete). I'm using the PowerShell Register-ScheduledJob cmdlet to create the job.

Everything works great except, by default, the Windows Task Scheduler will stop the script if it hasn't completed after 3 days. I can work around this by going in the GUI and unchecking the 'Stop the task if it runs longer than: 3 days' check box. I need to be able to 'uncheck' that box via Powershell code. Here's how I'm scheduling it currently:

$immediate = (get-date).AddMinutes(2).ToString("MM/dd/yyyy HH:mm")
$scheduled_date = get-date -Format "yyyyMMMd-HHMMss"
$trigger = New-JobTrigger -Once -At $immediate
$sjo = New-ScheduledJobOption -RunElevated
Register-ScheduledJob -Name "SVC Migrations - $scheduled_date" -ScriptBlock {powershell.exe -File C:\scripts\addvdiskcopy_queue.ps1 } -Trigger $trigger -ScheduledJobOption $sjo >> C:\scripts\temp\job_scheduled.txt

Again, everything works great with this until 72 hours hits. Any help is appreciated! Thanks!

like image 734
firestarter247 Avatar asked Nov 27 '22 21:11

firestarter247


2 Answers

New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0
like image 161
Prince Vultan Avatar answered Dec 06 '22 09:12

Prince Vultan


Just figured this out on Server 2012R2 without having to use the external DLL. Works with Register-ScheduledTask but haven't tested with Register-ScheduledJob.

I created the with Register-ScheduledTask in a way similar to the above but without the ExecutionTimeLimit setting defined. This gave me the default of 3 days.

Then I returned the task and changed the settings as below:

$Task = Get-ScheduledTask -TaskName "MyTask"
$Task.Settings.ExecutionTimeLimit = "PT0H"
Set-ScheduledTask $Task
like image 33
David Betts Avatar answered Dec 06 '22 09:12

David Betts