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:
- PowerShell version 5 is running on my local machine and both servers in this test.
- I am creating the scheduled task instance using
New-ScheduledTaskand 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).