Skip to content

Commit b6f03bc

Browse files
authored
Merge pull request #138 from GitBadSanta/setupdate
Issue 22: Updating scheduled task instead of Unregister/Register
2 parents 7363259 + e0c3919 commit b6f03bc

4 files changed

Lines changed: 77 additions & 45 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- xScheduledTask:
6+
- Update existing Scheduled Task using SetScheduleTask
7+
instead of UnRegister/Register - See [Issue #134](https://github.com/PowerShell/xComputerManagement/issues/134).
58
- Fix master branch AppVeyor badge link URL in README.MD - See [Issue #140](https://github.com/PowerShell/xComputerManagement/issues/140).
69
- Fix deletion of scheduled task with unknown or empty task trigger.
710
Get-TargetResource returns an empty ScheduleType string if the task

Modules/xComputerManagement/DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,7 @@ function Set-TargetResource
11271127
}
11281128

11291129
# Prepare the register arguments
1130-
$registerArguments = @{
1131-
TaskName = $TaskName
1132-
TaskPath = $TaskPath
1133-
}
1130+
$registerArguments = @{}
11341131

11351132
if ($PSBoundParameters.ContainsKey('ExecuteAsCredential'))
11361133
{
@@ -1178,18 +1175,29 @@ function Set-TargetResource
11781175
Principal = $principal
11791176
}
11801177

1178+
$tempScheduledTask = New-ScheduledTask @scheduledTaskArguments -ErrorAction Stop
1179+
11811180
if ($currentValues.Ensure -eq 'Present')
11821181
{
1183-
Write-Verbose -Message ($script:localizedData.RemovePreviousScheduledTaskMessage -f $TaskName, $TaskPath)
1184-
1185-
$null = Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false -ErrorAction Stop
1182+
Write-Verbose -Message ($script:localizedData.RetrieveScheduledTaskMessage -f $TaskName, $TaskPath)
1183+
$tempScheduledTask = New-ScheduledTask @scheduledTaskArguments -ErrorAction Stop
1184+
1185+
$scheduledTask = Get-ScheduledTask `
1186+
-TaskName $currentValues.TaskName `
1187+
-TaskPath $currentValues.TaskPath `
1188+
-ErrorAction Stop
1189+
$scheduledTask.Actions = $action
1190+
$scheduledTask.Triggers = $tempScheduledTask.Triggers
1191+
$scheduledTask.Settings = $setting
1192+
$scheduledTask.Principal = $principal
1193+
}
1194+
else
1195+
{
1196+
$scheduledTask = $tempScheduledTask
11861197
}
11871198

11881199
Write-Verbose -Message ($script:localizedData.CreateNewScheduledTaskMessage -f $TaskName, $TaskPath)
11891200

1190-
# Create the scheduled task object
1191-
$scheduledTask = New-ScheduledTask @scheduledTaskArguments -ErrorAction Stop
1192-
11931201
if ($repetition)
11941202
{
11951203
Write-Verbose -Message ($script:localizedData.SetRepetitionTriggerMessage -f $TaskName, $TaskPath)
@@ -1202,12 +1210,25 @@ function Set-TargetResource
12021210
$scheduledTask.Description = $Description
12031211
}
12041212

1205-
# Register the scheduled task
1206-
$registerArguments.Add('InputObject', $scheduledTask)
1213+
if ($currentValues.Ensure -eq 'Present')
1214+
{
1215+
# Updating the scheduled task
1216+
1217+
Write-Verbose -Message ($script:localizedData.UpdateScheduledTaskMessage -f $TaskName, $TaskPath)
1218+
$null = Set-ScheduledTask -InputObject $scheduledTask @registerArguments
1219+
}
1220+
else
1221+
{
1222+
Write-Verbose -Message ($script:localizedData.CreateNewScheduledTaskMessage -f $TaskName, $TaskPath)
12071223

1208-
Write-Verbose -Message ($script:localizedData.RegisterScheduledTaskMessage -f $TaskName, $TaskPath)
1224+
# Register the scheduled task
12091225

1210-
$null = Register-ScheduledTask @registerArguments -ErrorAction Stop
1226+
$registerArguments.Add('TaskName',$TaskName)
1227+
$registerArguments.Add('TaskPath',$TaskPath)
1228+
$registerArguments.Add('InputObject', $scheduledTask)
1229+
1230+
$null = Register-ScheduledTask @registerArguments
1231+
}
12111232
}
12121233

12131234
if ($Ensure -eq 'Absent')

Modules/xComputerManagement/DSCResources/MSFT_xScheduledTask/en-US/MSFT_xScheduledTask.strings.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ ConvertFrom-StringData @'
2222
CreateNewScheduledTaskMessage = Creating new scheduled task '{0}' in '{1}'.
2323
SetRepetitionTriggerMessage = Setting repetition trigger settings on task '{0}' in '{1}'.
2424
RegisterScheduledTaskMessage = Registering the scheduled task '{0}' in '{1}'.
25+
RetrieveScheduledTaskMessage = Retrieving the scheduled task '{0}' from '{1}'.
2526
RemoveScheduledTaskMessage = Removing scheduled task '{0}' from '{1}'.
27+
UpdateScheduledTaskMessage = Updating scheduled task '{0}' in '{1}'.
2628
TestScheduledTaskMessage = Testing scheduled task '{0}' in '{1}'.
2729
GetCurrentTaskValuesMessage = Current scheduled task values retrieved.
2830
CurrentTaskValuesNullMessage = Current scheduled values were null.

Tests/Unit/MSFT_xScheduledTask.Tests.ps1

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ try
6565
)
6666
}
6767

68+
# Function to allow mocking pipeline input
69+
function Set-ScheduledTask
70+
{
71+
param
72+
(
73+
[Parameter()]
74+
[switch]
75+
$Force,
76+
77+
[Parameter(ValueFromPipeline = $true)]
78+
$InputObject,
79+
80+
[Parameter()]
81+
[System.String]
82+
$Password,
83+
84+
[Parameter()]
85+
[System.String]
86+
$User
87+
)
88+
}
89+
6890
Describe $script:DSCResourceName {
6991
BeforeAll {
7092
Mock -CommandName Register-ScheduledTask
@@ -312,8 +334,7 @@ try
312334

313335
It 'Should update the scheduled task in the set method' {
314336
Set-TargetResource @testParameters
315-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
316-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
337+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
317338
}
318339
}
319340

@@ -412,8 +433,7 @@ try
412433

413434
It 'Should update the scheduled task in the set method' {
414435
Set-TargetResource @testParameters
415-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
416-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
436+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
417437
}
418438
}
419439

@@ -511,8 +531,7 @@ try
511531

512532
It 'Should update the scheduled task in the set method' {
513533
Set-TargetResource @testParameters
514-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
515-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
534+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
516535
}
517536
}
518537

@@ -608,8 +627,7 @@ try
608627

609628
It 'Should update the scheduled task in the set method' {
610629
Set-TargetResource @testParameters
611-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
612-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
630+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
613631
}
614632
}
615633

@@ -665,8 +683,7 @@ try
665683

666684
It 'Should update the scheduled task in the set method' {
667685
Set-TargetResource @testParameters
668-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
669-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
686+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
670687
}
671688
}
672689

@@ -722,8 +739,7 @@ try
722739

723740
It 'Should update the scheduled task in the set method' {
724741
Set-TargetResource @testParameters
725-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
726-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
742+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
727743
}
728744
}
729745

@@ -777,8 +793,7 @@ try
777793

778794
It 'Should update the scheduled task in the set method' {
779795
Set-TargetResource @testParameters
780-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
781-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
796+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
782797
}
783798
}
784799

@@ -832,8 +847,7 @@ try
832847

833848
It 'Should update the scheduled task in the set method' {
834849
Set-TargetResource @testParameters
835-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
836-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
850+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
837851
}
838852
}
839853

@@ -890,8 +904,7 @@ try
890904

891905
It 'Should update the scheduled task in the set method' {
892906
Set-TargetResource @testParameters
893-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
894-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
907+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
895908
}
896909

897910
}
@@ -951,8 +964,7 @@ try
951964

952965
It 'Should update the scheduled task in the set method' {
953966
Set-TargetResource @testParameters
954-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
955-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
967+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
956968
}
957969
}
958970

@@ -1125,8 +1137,7 @@ try
11251137

11261138
It 'Should update the scheduled task in the set method' {
11271139
Set-TargetResource @testParameters
1128-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1129-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
1140+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
11301141
}
11311142
}
11321143

@@ -1266,9 +1277,7 @@ try
12661277

12671278
It 'Should update the scheduled task in the set method' {
12681279
Set-TargetResource @testParameters
1269-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1270-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
1271-
}
1280+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1 }
12721281
}
12731282

12741283
Context 'A scheduled task exists and is configured with the wrong idle timeout & idle duration parameters' {
@@ -1334,8 +1343,7 @@ try
13341343

13351344
It 'Should update the scheduled task in the set method' {
13361345
Set-TargetResource @testParameters
1337-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1338-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
1346+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
13391347
}
13401348
}
13411349

@@ -1388,8 +1396,7 @@ try
13881396

13891397
It 'Should update the scheduled task in the set method' {
13901398
Set-TargetResource @testParameters
1391-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1392-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
1399+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
13931400
}
13941401
}
13951402

@@ -1442,8 +1449,7 @@ try
14421449

14431450
It 'Should update the scheduled task in the set method' {
14441451
Set-TargetResource @testParameters
1445-
Assert-MockCalled -CommandName Unregister-ScheduledTask -Exactly -Times 1
1446-
Assert-Mockcalled -CommandName Register-ScheduledTask -Exactly -Times 1
1452+
Assert-MockCalled -CommandName Set-ScheduledTask -Exactly -Times 1
14471453
}
14481454
}
14491455

0 commit comments

Comments
 (0)