[fixed]The task XML contains a value which is incorrectly formatted or out of range Duration:P99999999DT23H59M59S


Hi All,

When I am trying to create a scheduled task in windows server 2016, I was getting the below error.

The task XML contains a value which is incorrectly formatted or out of range.

(8,42):Duration:P99999999DT23H59M59S

+ CategoryInfo : NotSpecified: (PS_ScheduledTask:Root/Microsoft/..

.S_ScheduledTask) [Register-ScheduledTask], CimException

+ FullyQualifiedErrorId : HRESULT 0x80041318,Register-ScheduledTask

+ PSComputerName : AZWEPSSCBTXPFE1.partners.extranet.microsoft.com

After looking online, found this issue with [timespan]::MaxValue property that used in the script

This was my script.

$mycredentials = Get-Credential

Invoke-Command -ComputerName “Server1” -Credential $mycredentials -ScriptBlock {

$Action = New-ScheduledTaskAction -Execute ‘powershell.exe’ -Argument ‘-command “D:\PS_Jobs\PS_Job1.ps1” -ExecutionPolicy RemoteSigned -NoProfile’

$Trigger = New-ScheduledTaskTrigger -Once -At 9am -RandomDelay (New-TimeSpan -Minutes 30) -RepetitionDuration ([timespan]::MaxValue) -RepetitionInterval (New-TimeSpan -Minutes 60)

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)

$Task | Register-ScheduledTask -TaskName ‘Job1 Task’

}

As the issue with [timespan]::MaxValue, I made some changes to include the time span for only 3 years to run the script.

This is the updated and working script.

Invoke-Command -ComputerName “Server1” -Credential $mycredentials -ScriptBlock {

$dt= ([DateTime]::Now)

$timespan = $dt.AddYears(3) -$dt;

$Action = New-ScheduledTaskAction -Execute ‘powershell.exe’ -Argument ‘-command “D:\PS_Jobs\PS_Job1.ps1” -ExecutionPolicy RemoteSigned -NoProfile’

$Trigger = New-ScheduledTaskTrigger -Once -At 9am -RandomDelay (New-TimeSpan -Minutes 30) -RepetitionDuration $timespan -RepetitionInterval (New-TimeSpan -Minutes 60)

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)

$Task | Register-ScheduledTask -TaskName ‘Job1 Task’

}

Regards,

Chaitanya

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: