Skip to content

Commit 995a164

Browse files
Vasily LarionovVasily Larionov
authored andcommitted
Fixed root path
Fixed tests to rely on TaskPath
1 parent 6a11761 commit 995a164

2 files changed

Lines changed: 58 additions & 33 deletions

File tree

DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ function Get-TargetResource
4949
$ExecuteAsCredential
5050
)
5151

52-
$fixedTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\"
53-
54-
$task = Get-ScheduledTask -TaskName $TaskName -TaskPath $fixedTaskPath -ErrorAction SilentlyContinue
52+
if($TaskPath -eq '\')
53+
{
54+
$fixedTaskPath = '\'
55+
}
56+
else
57+
{
58+
$fixedTaskPath = "\$(($TaskPath -split '\\').Where({$_}) -join '\')\"
59+
60+
}
5561

62+
$task = Get-ScheduledTask -TaskName $TaskName -TaskPath $fixedTaskPath -ErrorAction SilentlyContinue
63+
5664
if ($null -eq $task)
5765
{
5866
return @{

Tests/Unit/MSFT_xScheduledTask.Tests.ps1

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ try
3636
Context "No scheduled task exists, but it should" {
3737
$testParams = @{
3838
TaskName = "Test task"
39+
TaskPath = '\Test\'
3940
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
4041
ScheduleType = "Minutes"
4142
RepeatInterval = 15
@@ -59,15 +60,16 @@ try
5960
Context "A scheduled task exists, but it shouldn't" {
6061
$testParams = @{
6162
TaskName = "Test task"
63+
TaskPath = '\Test\'
6264
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
6365
ScheduleType = "Minutes"
6466
RepeatInterval = 15
6567
Ensure = "Absent"
6668
}
6769

6870
Mock Get-ScheduledTask { return @{
69-
Name = $testParams.TaskName
70-
Path = $testParams.TaskPath
71+
TaskName = $testParams.TaskName
72+
TaskPath = $testParams.TaskPath
7173
Actions = @(@{
7274
Execute = $testParams.ActionExecutable
7375
})
@@ -99,6 +101,7 @@ try
99101
Context "A scheduled task doesnt exist, and it shouldn't" {
100102
$testParams = @{
101103
TaskName = "Test task"
104+
TaskPath = '\Test\'
102105
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
103106
ScheduleType = "Minutes"
104107
RepeatInterval = 15
@@ -119,14 +122,15 @@ try
119122
Context "A scheduled task with minutes based repetition exists, but has the wrong settings" {
120123
$testParams = @{
121124
TaskName = "Test task"
125+
TaskPath = '\Test\'
122126
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
123127
ScheduleType = "Minutes"
124128
RepeatInterval = 15
125129
}
126130

127131
Mock Get-ScheduledTask { return @{
128-
Name = $testParams.TaskName
129-
Path = $testParams.TaskPath
132+
TaskName = $testParams.TaskName
133+
TaskPath = $testParams.TaskPath
130134
Actions = @(@{
131135
Execute = $testParams.ActionExecutable
132136
})
@@ -158,14 +162,15 @@ try
158162
Context "A scheduled task with minutes based repetition exists and has the correct settings" {
159163
$testParams = @{
160164
TaskName = "Test task"
165+
TaskPath = '\Test\'
161166
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
162167
ScheduleType = "Minutes"
163168
RepeatInterval = 15
164169
}
165170

166171
Mock Get-ScheduledTask { return @{
167-
Name = $testParams.TaskName
168-
Path = $testParams.TaskPath
172+
TaskName = $testParams.TaskName
173+
TaskPath = $testParams.TaskPath
169174
Actions = @(@{
170175
Execute = $testParams.ActionExecutable
171176
})
@@ -192,14 +197,15 @@ try
192197
Context "A scheduled task with hourly based repetition exists, but has the wrong settings" {
193198
$testParams = @{
194199
TaskName = "Test task"
200+
TaskPath = '\Test\'
195201
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
196202
ScheduleType = "Hourly"
197203
RepeatInterval = 4
198204
}
199205

200206
Mock Get-ScheduledTask { return @{
201-
Name = $testParams.TaskName
202-
Path = $testParams.TaskPath
207+
TaskName = $testParams.TaskName
208+
TaskPath = $testParams.TaskPath
203209
Actions = @(@{
204210
Execute = $testParams.ActionExecutable
205211
})
@@ -231,14 +237,15 @@ try
231237
Context "A scheduled task with hourly based repetition exists and has the correct settings" {
232238
$testParams = @{
233239
TaskName = "Test task"
240+
TaskPath = '\Test\'
234241
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
235242
ScheduleType = "Hourly"
236243
RepeatInterval = 4
237244
}
238245

239246
Mock Get-ScheduledTask { return @{
240-
Name = $testParams.TaskName
241-
Path = $testParams.TaskPath
247+
TaskName = $testParams.TaskName
248+
TaskPath = $testParams.TaskPath
242249
Actions = @(@{
243250
Execute = $testParams.ActionExecutable
244251
})
@@ -265,14 +272,15 @@ try
265272
Context "A scheduled task with daily based repetition exists, but has the wrong settings" {
266273
$testParams = @{
267274
TaskName = "Test task"
275+
TaskPath = '\Test\'
268276
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
269277
ScheduleType = "Daily"
270278
RepeatInterval = 3
271279
}
272280

273281
Mock Get-ScheduledTask { return @{
274-
Name = $testParams.TaskName
275-
Path = $testParams.TaskPath
282+
TaskName = $testParams.TaskName
283+
TaskPath = $testParams.TaskPath
276284
Actions = @(@{
277285
Execute = $testParams.ActionExecutable
278286
})
@@ -304,14 +312,15 @@ try
304312
Context "A scheduled task with daily based repetition exists and has the correct settings" {
305313
$testParams = @{
306314
TaskName = "Test task"
315+
TaskPath = '\Test\'
307316
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
308317
ScheduleType = "Daily"
309318
RepeatInterval = 3
310319
}
311320

312321
Mock Get-ScheduledTask { return @{
313-
Name = $testParams.TaskName
314-
Path = $testParams.TaskPath
322+
TaskName = $testParams.TaskName
323+
TaskPath = $testParams.TaskPath
315324
Actions = @(@{
316325
Execute = $testParams.ActionExecutable
317326
})
@@ -338,15 +347,16 @@ try
338347
Context "A scheduled task exists and is configured with the wrong execution account" {
339348
$testParams = @{
340349
TaskName = "Test task"
350+
TaskPath = '\Test\'
341351
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
342352
ScheduleType = "Minutes"
343353
RepeatInterval = 15
344354
ExecuteAsCredential = New-Object System.Management.Automation.PSCredential ("DEMO\RightUser", (ConvertTo-SecureString "ExamplePassword" -AsPlainText -Force))
345355
}
346356

347357
Mock Get-ScheduledTask { return @{
348-
Name = $testParams.TaskName
349-
Path = $testParams.TaskPath
358+
TaskName = $testParams.TaskName
359+
TaskPath = $testParams.TaskPath
350360
Actions = @(@{
351361
Execute = $testParams.ActionExecutable
352362
})
@@ -378,15 +388,16 @@ try
378388
Context "A scheduled task exists and is configured with the wrong working directory" {
379389
$testParams = @{
380390
TaskName = "Test task"
391+
TaskPath = '\Test\'
381392
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
382393
ActionWorkingPath = "C:\Example"
383394
ScheduleType = "Minutes"
384395
RepeatInterval = 15
385396
}
386397

387398
Mock Get-ScheduledTask { return @{
388-
Name = $testParams.TaskName
389-
Path = $testParams.TaskPath
399+
TaskName = $testParams.TaskName
400+
TaskPath = $testParams.TaskPath
390401
Actions = @(@{
391402
Execute = $testParams.ActionExecutable
392403
WorkingDirectory = "C:\Wrong"
@@ -419,15 +430,16 @@ try
419430
Context "A scheduled task exists and is configured with the wrong executable arguments" {
420431
$testParams = @{
421432
TaskName = "Test task"
433+
TaskPath = '\Test\'
422434
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
423435
ActionArguments = "-File `"C:\something\right.ps1`""
424436
ScheduleType = "Minutes"
425437
RepeatInterval = 15
426438
}
427439

428440
Mock Get-ScheduledTask { return @{
429-
Name = $testParams.TaskName
430-
Path = $testParams.TaskPath
441+
TaskName = $testParams.TaskName
442+
TaskPath = $testParams.TaskPath
431443
Actions = @(@{
432444
Execute = $testParams.ActionExecutable
433445
Arguments = "-File `"C:\something\wrong.ps1`""
@@ -460,15 +472,16 @@ try
460472
Context "A scheduled task is enabled and should be disabled" {
461473
$testParams = @{
462474
TaskName = "Test task"
475+
TaskPath = '\Test\'
463476
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
464477
ScheduleType = "Minutes"
465478
RepeatInterval = 15
466479
Enable = $false
467480
}
468481

469482
Mock Get-ScheduledTask { return @{
470-
Name = $testParams.TaskName
471-
Path = $testParams.TaskPath
483+
TaskName = $testParams.TaskName
484+
TaskPath = $testParams.TaskPath
472485
Actions = @(@{
473486
Execute = $testParams.ActionExecutable
474487
Arguments = $testParams.Arguments
@@ -505,15 +518,16 @@ try
505518
Context "A scheduled task is enabled and has the correct settings" {
506519
$testParams = @{
507520
TaskName = "Test task"
521+
TaskPath = '\Test\'
508522
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
509523
ScheduleType = "Minutes"
510524
RepeatInterval = 15
511525
Enable = $true
512526
}
513527

514528
Mock Get-ScheduledTask { return @{
515-
Name = $testParams.TaskName
516-
Path = $testParams.TaskPath
529+
TaskName = $testParams.TaskName
530+
TaskPath = $testParams.TaskPath
517531
Actions = @(@{
518532
Execute = $testParams.ActionExecutable
519533
Arguments = $testParams.Arguments
@@ -544,15 +558,16 @@ try
544558
Context "A scheduled task is disabled and has the correct settings" {
545559
$testParams = @{
546560
TaskName = "Test task"
561+
TaskPath = '\Test\'
547562
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
548563
ScheduleType = "Minutes"
549564
RepeatInterval = 15
550565
Enable = $false
551566
}
552567

553568
Mock Get-ScheduledTask { return @{
554-
Name = $testParams.TaskName
555-
Path = $testParams.TaskPath
569+
TaskName = $testParams.TaskName
570+
TaskPath = $testParams.TaskPath
556571
Actions = @(@{
557572
Execute = $testParams.ActionExecutable
558573
Arguments = $testParams.Arguments
@@ -583,15 +598,16 @@ try
583598
Context "A scheduled task is disabled but should be enabled" {
584599
$testParams = @{
585600
TaskName = "Test task"
601+
TaskPath = '\Test\'
586602
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
587603
ScheduleType = "Minutes"
588604
RepeatInterval = 15
589605
Enable = $true
590606
}
591607

592608
Mock Get-ScheduledTask { return @{
593-
Name = $testParams.TaskName
594-
Path = $testParams.TaskPath
609+
TaskName = $testParams.TaskName
610+
TaskPath = $testParams.TaskPath
595611
Actions = @(@{
596612
Execute = $testParams.ActionExecutable
597613
Arguments = $testParams.Arguments
@@ -627,14 +643,15 @@ try
627643
Context "A Scheduled task exists, is disabled, and the optional parameter enable is not specified" -Fixture {
628644
$testParams = @{
629645
TaskName = "Test task"
646+
TaskPath = '\Test\'
630647
ActionExecutable = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
631648
ScheduleType = "Minutes"
632649
RepeatInterval = 15
633650
}
634651

635652
Mock Get-ScheduledTask { return @{
636-
Name = $testParams.TaskName
637-
Path = $testParams.TaskPath
653+
TaskName = $testParams.TaskName
654+
TaskPath = $testParams.TaskPath
638655
Actions = @(@{
639656
Execute = $testParams.ActionExecutable
640657
Arguments = $testParams.Arguments

0 commit comments

Comments
 (0)