@@ -735,7 +735,7 @@ function Set-TargetResource
735735 }
736736
737737 # To overcome the issue of not being able to set the task repetition for tasks with a schedule type other than Once
738- if ($RepeatInterval.TimeOfDay -gt (New-TimeSpan - Seconds 0 ))
738+ if ($RepeatInterval.TimeOfDay -gt (New-TimeSpan - Seconds 0 ) -and $PSVersionTable .PSVersion.Major -gt 4 )
739739 {
740740 if ($RepetitionDuration.TimeOfDay -le $RepeatInterval.TimeOfDay )
741741 {
@@ -747,17 +747,8 @@ function Set-TargetResource
747747
748748 $tempTrigger = New-ScheduledTaskTrigger - Once - At 6 :6 :6 - RepetitionInterval $RepeatInterval.TimeOfDay - RepetitionDuration $RepetitionDuration.TimeOfDay
749749 Write-Verbose - Message ' Copying values from temporary trigger to property Repetition of $trigger.Repetition'
750- try {
751- $trigger.CimInstanceProperties [' Repetition' ].Value = $tempTrigger.Repetition
752- }
753- catch {
754- try {
755- $trigger.Repetition = $tempTrigger.Repetition
756- }
757- catch {
758- throw $PSItem
759- }
760- }
750+
751+ $trigger.Repetition = $tempTrigger.Repetition
761752 }
762753
763754 if ($currentValues.Ensure -eq " Present" )
@@ -769,6 +760,24 @@ function Set-TargetResource
769760 Write-Verbose - Message (' Creating new scheduled task' -f $TaskName )
770761
771762 $scheduledTask = New-ScheduledTask - Action $action - Trigger $trigger - Settings $setting
763+
764+ if ($RepeatInterval.TimeOfDay -gt (New-TimeSpan - Seconds 0 ) -and $PSVersionTable.PSVersion.Major -eq 4 )
765+ {
766+ if ($RepetitionDuration.TimeOfDay -le $RepeatInterval.TimeOfDay )
767+ {
768+ $exceptionObject = New-Object System.ArgumentException - ArgumentList `
769+ (' Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay , $RepetitionDuration.TimeOfDay ),`
770+ ' RepetitionDuration'
771+ throw $exceptionObject
772+ }
773+
774+ $tempTrigger = New-ScheduledTaskTrigger - Once - At 6 :6 :6 - RepetitionInterval $RepeatInterval.TimeOfDay - RepetitionDuration $RepetitionDuration.TimeOfDay
775+ $tempTask = New-ScheduledTask - Trigger $temptrigger - Action $action
776+ Write-Verbose - Message ' Copying values from temporary trigger to property Repetition of $trigger.Repetition'
777+
778+ $scheduledTask.Triggers [0 ].Repetition = $tempTask.Triggers [0 ].Repetition
779+ }
780+
772781 if (-not [string ]::IsNullOrWhiteSpace($Description ))
773782 {
774783 $scheduledTask.Description = $Description
0 commit comments