Question

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!

Was it helpful?

Solution 3

Check out this thread on CodePlex.

It looks like you can use the Task Scheduler Managed Library to achieve this. You'll need to download the library and load the DLL. Here is a fully working sample (just update the path to the DLL).

$TaskName = 'asdf';

# Unregister the Scheduled Job if it exists
Get-ScheduledJob asdf -ErrorAction SilentlyContinue | Unregister-ScheduledJob;

# Create the Scheduled Job
$Trigger = New-JobTrigger -At '5:00 PM' -Once;
$Option = New-ScheduledJobOption;
$Action = { Write-Host 'hi'; };
$Job = Register-ScheduledJob -Name asdf -ScriptBlock $Action -Trigger $Trigger -ScheduledJobOption $Option;

# Modify the Scheduled Job using external library
Add-Type -Path "C:\Users\Trevor\Downloads\TaskScheduler\Microsoft.Win32.TaskScheduler.dll";
$TaskService = New-Object -TypeName Microsoft.Win32.TaskScheduler.TaskService;
$Task = $TaskService.FindTask($TaskName, $true);
$Task.Definition.Settings.ExecutionTimeLimit = [System.TimeSpan]::Zero;
$Task.RegisterChanges();

I tested the library in my environment, and it works as expected. The net result is that the "Stop the task if it runs longer than" checkbox is disabled.

Task Scheduler

OTHER TIPS

New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0

This returns an object that can be passed to Register-ScheduledJob's -Settings argument. For example:

Register-ScheduledJob `
    -Settings $(New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0) `
    # ...further arguments...

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

Use the following settings in task definition:

New-ScheduledTaskSettingsSet -ExecutionTimeLimit "PT0S"

Older post, but none of these options worked for me other than what @jdc0589 said in a comment. I run this script on an Azure VM running Windows Server 2016 Datacenter.

$action = New-ScheduledTaskAction -Execute "java.exe" `
    -Argument "-jar `"D:\Selenium\selenium-grid-extras.jar`"" `
    -WorkingDirectory "D:\Selenium"

$trigger = New-ScheduledTaskTrigger -AtStartup

$everyMinute = New-TimeSpan -Minutes 1
$nolimit = New-TimeSpan -Minutes 0
$settings = New-ScheduledTaskSettingsSet `
    -MultipleInstances IgnoreNew `
    -RestartInterval $everyMinute `
    -RestartCount 999 `
    -Priority 0 `
    -ExecutionTimeLimit $nolimit `
    -StartWhenAvailable `
    -DisallowHardTerminate

Register-ScheduledTask `
    -Action $action `
    -Trigger $trigger `
    -Settings $settings `
    -TaskName "Start SGE Hub" `
    -TaskPath "\Selenium Grid Extras" `
    -Description "At machine startup, run SGE so that the hub can process tests without a logon" `
    -RunLevel Highest `
    -User "SYSTEM" `
    -Force

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top