Skip to content

Commit dffb78b

Browse files
committed
Used new functions to throw exceptions
1 parent 2365c7c commit dffb78b

1 file changed

Lines changed: 16 additions & 27 deletions

File tree

DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ function Get-TargetResource
322322

323323
default
324324
{
325-
throw "Trigger type $_ not recognized."
325+
New-InvalidArgumentException -Message "Trigger type $_ not recognized." -ArgumentName CimClassName
326326
}
327327
}
328328

@@ -657,7 +657,7 @@ function Get-TargetResource
657657
#>
658658
function Set-TargetResource
659659
{
660-
param
660+
param
661661
(
662662
[Parameter(Mandatory = $true)]
663663
[System.String]
@@ -827,33 +827,26 @@ param
827827
{
828828
if ($RepetitionDuration.TimeOfDay -lt $RepeatInterval.TimeOfDay)
829829
{
830-
$exceptionObject = New-Object -TypeName System.ArgumentException -ArgumentList `
831-
('Repetition duration {0} is less than repetition interval {1}. Please set RepeatInterval to a value lower or equal to RepetitionDuration' -f $RepetitionDuration.TimeOfDay, $RepeatInterval.TimeOfDay), `
832-
'RepeatInterval'
833-
throw $exceptionObject
830+
$exceptionMessage = 'Repetition duration {0} is less than repetition interval {1}. Please set RepeatInterval to a value lower or equal to RepetitionDuration' -f $RepetitionDuration.TimeOfDay, $RepeatInterval.TimeOfDay
831+
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName RepeatInterval
834832
}
835833

836834
if ($ScheduleType -eq 'Daily' -and $DaysInterval -eq 0)
837835
{
838-
$exceptionObject = New-Object -TypeName System.ArgumentException -ArgumentList `
839-
('Schedules of the type Daily must have a DaysInterval greater than 0 (value entered: {0})' -f $DaysInterval), `
840-
'DaysInterval'
841-
throw $exceptionObject
836+
$exceptionMessage = 'Schedules of the type Daily must have a DaysInterval greater than 0 (value entered: {0})' -f $DaysInterval
837+
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName DaysInterval
842838
}
843839

844840
if ($ScheduleType -eq 'Weekly' -and $WeeksInterval -eq 0)
845841
{
846-
$exceptionObject = New-Object -TypeName System.ArgumentException -ArgumentList `
847-
('Schedules of the type Weekly must have a WeeksInterval greater than 0 (value entered: {0})' -f $WeeksInterval), `
848-
'WeeksInterval'
849-
throw $exceptionObject
842+
$exceptionMessage = 'Schedules of the type Weekly must have a WeeksInterval greater than 0 (value entered: {0})' -f $WeeksInterval
843+
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName WeeksInterval
850844
}
851845

852846
if ($ScheduleType -eq 'Weekly' -and $DaysOfWeek.Count -eq 0)
853847
{
854-
$exceptionObject = New-Object -TypeName System.ArgumentException -ArgumentList `
855-
'Schedules of the type Weekly must have at least one weekday selected', 'DaysOfWeek'
856-
throw $exceptionObject
848+
$exceptionMessage = 'Schedules of the type Weekly must have at least one weekday selected'
849+
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName DaysOfWeek
857850
}
858851

859852
$actionArgs = @{
@@ -973,18 +966,16 @@ param
973966
$trigger = New-ScheduledTaskTrigger @triggerArgs -ErrorAction SilentlyContinue
974967
if (-not $trigger)
975968
{
976-
throw "Error creating new scheduled task trigger. $($_.Exception.Message)"
969+
New-InvalidOperationException -Message 'Error creating new scheduled task trigger' -ErrorRecord $_
977970
}
978971

979972
# To overcome the issue of not being able to set the task repetition for tasks with a schedule type other than Once
980973
if ($RepeatInterval.TimeOfDay -gt (New-TimeSpan -Seconds 0) -and $PSVersionTable.PSVersion.Major -gt 4)
981974
{
982975
if ($RepetitionDuration.TimeOfDay -le $RepeatInterval.TimeOfDay)
983976
{
984-
$exceptionObject = New-Object System.ArgumentException -ArgumentList `
985-
('Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay), `
986-
'RepetitionDuration'
987-
throw $exceptionObject
977+
$exceptionMessage ='Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay
978+
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName RepetitionDuration
988979
}
989980

990981
$tempTrigger = New-ScheduledTaskTrigger -Once -At 6:6:6 -RepetitionInterval $RepeatInterval.TimeOfDay -RepetitionDuration $RepetitionDuration.TimeOfDay
@@ -1014,10 +1005,8 @@ param
10141005
{
10151006
if ($RepetitionDuration.TimeOfDay -le $RepeatInterval.TimeOfDay)
10161007
{
1017-
$exceptionObject = New-Object System.ArgumentException -ArgumentList `
1018-
('Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay), `
1019-
'RepetitionDuration'
1020-
throw $exceptionObject
1008+
$exceptionMessage = 'Repetition interval is set to {0} but repetition duration is {1}' -f $RepeatInterval.TimeOfDay, $RepetitionDuration.TimeOfDay
1009+
New-InvalidArgumentException -Message $exceptionMessage -ArgumentName RepetitionDuration
10211010
}
10221011

10231012
$tempTrigger = New-ScheduledTaskTrigger -Once -At 6:6:6 -RepetitionInterval $RepeatInterval.TimeOfDay -RepetitionDuration $RepetitionDuration.TimeOfDay
@@ -1326,6 +1315,6 @@ function Test-TargetResource
13261315
return $false
13271316
}
13281317

1329-
Write-Verbose 'Testing DSC parameter state'
1318+
Write-Verbose -Message 'Testing DSC parameter state'
13301319
return Test-DscParameterState -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters
13311320
}

0 commit comments

Comments
 (0)