I have a script that can legally run for much longer than 3 days (this is a queueing script command, so there are many pauses in it waiting for the task to complete). I use the PowerShell cmdlet Register-ScheduledJobto create a job.
Everything works fine, except that by default the Windows Task Scheduler will stop the script if it does not complete after 3 days. I can get around this by going to the GUI and unchecking the "Stop the task if it works longer than: 3 days" checkbox. I need to uncheck using Powershell code. Here's how I plan it now:
$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 fine with this up to 72 hours. Any help is appreciated! Thank!
source
share