Powershell: New-ScheduledTaskTrigger cmdlet with indefinite duration in Windows 10 and windows 2016 server

chandan vysyaraju 21 Reputation points
2020-10-30T05:15:47.593+00:00

Goal: To add a new trigger to a schedule task that runs every 3 minutes for an indefinite duration.

Please note the commands should work on both windows 2012 server and windows 2016 server.

I am using New-ScheduledTaskTrigger cmdlet to create a new trigger object as below

New-ScheduledTaskTrigger -Once -At $startTime -RepetitionInterval (New-TimeSpan -Minutes 3) -RepetitionDuration ([TimeSpan]::MaxValue)

And using this trigger with Set-ScheduledTask.

Windows 2016 server behavior:

  • An error is thrown as below

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

  • But removing the "RepetitionDuration" parameter would work as expected and creates a trigger with indefinite duration.

Windows 2012 server behavior:

  • New-ScheduledTasTrigger command as provided above works fine
  • But if I remove "RepetitionDuration" parameter (to make it work on windows 2016), this throws an error as below

New-ScheduledTaskTrigger : The RepetitionInterval and RepetitionDuration Job trigger parameters must be specified together.

Please help me to resolve this. Let me know in case any additional information is required.

Powershell version information in windows 2012 (Output of $PSVersionTable)

Name Value


PSVersion 5.1.14409.1005

PSEdition Desktop

PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}

BuildVersion 10.0.14409.1005

CLRVersion 4.0.30319.42000

WSManStackVersion 3.0

PSRemotingProtocolVersion 2.3

SerializationVersion 1.1.0.1

Powershell version information in windows 2016 (Output of $PSVersionTable)

Name Value


PSVersion 5.1.14393.3471

PSEdition Desktop

PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}

BuildVersion 10.0.14393.3471

CLRVersion 4.0.30319.42000

WSManStackVersion 3.0

PSRemotingProtocolVersion 2.3

SerializationVersion 1.1.0.1

Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,507 questions
Windows Server 2012
Windows Server 2012
A Microsoft server operating system that supports enterprise-level management, data storage, applications, and communications.
1,589 questions
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,524 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 36,836 Reputation points Microsoft Vendor
    2020-10-30T10:15:06.92+00:00

    Hi,

    If you want the script works on both windows server 2012 and 2016, you could use different parameters based on the os version.

    $starttime=(get-date)  
    $Params2012 = @{  
        "Once" = $true  
        "At" = $startTime  
        "RepetitionInterval" = (New-TimeSpan -Minutes 3)  
        "RepetitionDuration" = ([TimeSpan]::MaxValue)  
    }  
    $Params2016 = @{  
        "Once" = $true  
        "At" = $startTime  
        "RepetitionInterval" = (New-TimeSpan -Minutes 3)  
    }  
    if([environment]::OSVersion.Version.Major -eq 10){$Params = $Params2016}  
    if([environment]::OSVersion.Version.Major -eq 6){$Params = $Params2012}  
    $trigger = New-ScheduledTaskTrigger @Params  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,711 Reputation points
    2020-10-30T18:33:26.177+00:00

    The format of the timespan looks correct, but perhaps the range is the cause of the problem. I took a quick look but I didn't see any maximum value for scheduled class.

    Why not experiment a bit and see if reducing the duration to something a bit shorter than the maximum possible value? Maybe start with an approximation of 500 years?

    new-timespan -Days (365*500)

    0 comments No comments

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.