Scheduled task via PowerShell ends in 3 days

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!

+4
source share
5 answers

Check this thread in CodePlex .

It looks like you can use the managed task scheduler library to achieve this. You will need to load the library and load the DLL. Here is a fully working example (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 end result is that the "Stop the task if it works longer" checkbox is disabled.

Task scheduler

+5
source

Just figured it out on the 2012R2 server without using an external DLL. Works with Register-ScheduledTask, but not verified with Register-ScheduledJob.

Register-ScheduledTask , , ExecutionTimeLimit. 3 .

, :

$Task = Get-ScheduledTask -TaskName "MyTask"
$Task.Settings.ExecutionTimeLimit = "PT0H"
Set-ScheduledTask $Task
+2

:

New-ScheduledTaskSettingsSet -ExecutionTimeLimit "PT0S"
+1
New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0
+1

, , , @ddc0589 . script Azure VM, 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

0

Source: https://habr.com/ru/post/1535611/


All Articles