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!
New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With