0

I have the following lines of PowerShell code that I'm using to register a scheduled task.

$User = "user"
$Password = "password"
$TaskPath = "<scheduled_task_folder>"

$TaskName = "hello_world"
$TaskDescription = "Run Hello World"
$TaskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\source_code\scripts\hello_world.ps1"
$TaskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([System.TimeSpan]::MaxValue)
$TaskPrincipal = New-ScheduledTaskPrincipal -Id $User -UserId $User -LogonType Password -RunLevel Limited
$Task = New-ScheduledTask -Description $TaskDescription -Action $TaskAction -Principal $TaskPrincipal -Trigger $TaskTrigger
$Task | Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -User $User -Password $Password | Out-Null

When I run this code on a Windows 2012 R2 server it works without any problem and the scheduled task works as expected.

However, when I run the same code on a Windows 2016 server (or a Windows 10 laptop) I get the following error message

The task XML contains a value which is incorrectly formatted or out of range.(10,42):Duration:P99999999DT23H59M59S

Can anyone tell my why the code won't work on the later version of Windows.

Notes:

  1. PowerShell version 5 is running on my local machine and both servers in this test.
  2. I am creating the scheduled task instance using New-ScheduledTask and then registering the task so that the scheduled task will still run when not logged into the server (see https://stackoverflow.com/a/70793765/3095259 for a very detailed explanation of this).
gerard
  • 835
  • 11
  • 27
  • 1
    Does this answer your question? [Powershell: Scheduled Task with Daily Trigger and Repetition Interval](https://stackoverflow.com/questions/20108886/powershell-scheduled-task-with-daily-trigger-and-repetition-interval). Specifically, the first answer and its comments. – Jeroen Mostert Apr 20 '22 at 15:16
  • @JeroenMostert You sir, are a lifesaver. That did the trick. – gerard Apr 20 '22 at 15:51

0 Answers0