Writing a powershell for a single automation and getting the error: "Register-ScheduledTask : The task XML contains a value which is incorrectly formatted or out of range."

נאור אור ציון 26 Reputation points
2022-07-07T11:50:39.587+00:00

I'm trying to create a task through PowerShell script, I only want to do it once and prefer not to learn the full syntax.

$action = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-NonInteractive -NoLogo -NoProfile -File "C:\Users\Nia\Desktop\Brute-force-ctf\hint.txt"'  
$trigger = New-ScheduledTaskTrigger -Once -At 11pm -RepetitionDuration  ([TimeSpan]::MaxValue)  -RepetitionInterval  (New-TimeSpan -Days 1)  
$settings = New-ScheduledTaskSettingsSet  
Register-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -TaskName "test" -Description "This is a test"  

After executing it I'm getting the following error:

Register-ScheduledTask : The task XML contains a value which is incorrectly formatted or out of range.

(10,42):Duration:P99999999DT23H59M59S
At C:\Users\Nia\Desktop\Brute-force-ctf\test.ps1:4 char:1

  • Register-ScheduledTask -Action $action -Trigger $trigger -Settings $s ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTa
    sk], CimException
  • FullyQualifiedErrorId : HRESULT 0x80041318,Register-ScheduledTask

I don't understand why the error and how to fix it.
Thanks ahead for the help!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,629 questions
0 comments No comments
{count} vote

Accepted answer
  1. DaveK 1,866 Reputation points
    2022-07-07T13:01:54.567+00:00

    Hi,

    This maybe OS dependant and its relating to -RepetitionDuration. I believe on Win10/Server 2016+ to create it to run indefinitely you omit the -RepetitionDuration and only need to add in the interval

    $action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument "-NonInteractive -NoLogo -NoProfile -File 'C:\Users\Nia\Desktop\Brute-force-ctf\hint.txt'"  
    $trigger = New-ScheduledTaskTrigger -Once -At 11pm -RepetitionInterval (New-TimeSpan -Days 1)  
    $settings = New-ScheduledTaskSettingsSet  
    Register-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -TaskName "test" -Description "This is a test"  
    

    You don't mention where your using the script so it maybe you need to put OS checks in place depending on if its a Server 2016 or newer / Win 10 system.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.