Skip to content

Commit 8ac2d43

Browse files
committed
Code review feedback
1 parent 6a8f650 commit 8ac2d43

3 files changed

Lines changed: 233 additions & 80 deletions

File tree

DSCResources/MSFT_xScheduledTask/MSFT_xScheduledTask.psm1

Lines changed: 225 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,92 @@ function Get-TargetResource
33
[OutputType([System.Collections.Hashtable])]
44
param
55
(
6-
[Parameter(Mandatory=$true)] [System.String] $TaskName,
7-
[Parameter(Mandatory=$false)] [System.String] $TaskPath = "\",
8-
[Parameter(Mandatory=$true)] [System.String] $ActionExecutable,
9-
[Parameter(Mandatory=$false)] [System.String] $ActionArguments,
10-
[Parameter(Mandatory=$false)] [System.String] $ActionWorkingPath,
11-
[Parameter(Mandatory=$true)] [System.String] [ValidateSet("Minutes", "Hourly", "Daily")] $ScheduleType,
12-
[Parameter(Mandatory=$true)] [System.UInt32] $RepeatInterval,
13-
[Parameter(Mandatory=$false)] [System.String] $StartTime = "12:00 AM",
14-
[Parameter(Mandatory=$false)] [System.String] [ValidateSet("Present","Absent")] $Ensure = "Present",
15-
[Parameter(Mandatory=$false)] [System.Management.Automation.PSCredential] $ExecuteAsCredential
6+
[Parameter(Mandatory=$true)]
7+
[System.String]
8+
$TaskName,
9+
10+
[Parameter(Mandatory=$false)]
11+
[System.String]
12+
$TaskPath = "\",
13+
14+
[Parameter(Mandatory=$true)]
15+
[System.String]
16+
$ActionExecutable,
17+
18+
[Parameter(Mandatory=$false)]
19+
[System.String]
20+
$ActionArguments,
21+
22+
[Parameter(Mandatory=$false)]
23+
[System.String]
24+
$ActionWorkingPath,
25+
26+
[Parameter(Mandatory=$true)]
27+
[System.String]
28+
[ValidateSet("Minutes", "Hourly", "Daily")] $ScheduleType,
29+
30+
[Parameter(Mandatory=$true)]
31+
[System.UInt32]
32+
$RepeatInterval,
33+
34+
[Parameter(Mandatory=$false)]
35+
[System.String]
36+
$StartTime = "12:00 AM",
37+
38+
[Parameter(Mandatory=$false)]
39+
[System.String]
40+
[ValidateSet("Present","Absent")]
41+
$Ensure = "Present",
42+
43+
[Parameter(Mandatory=$false)]
44+
[System.Management.Automation.PSCredential]
45+
$ExecuteAsCredential
1646
)
1747
$task = Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue
1848

19-
if ($null -eq $task) {
49+
if ($null -eq $task)
50+
{
2051
return @{
2152
TaskName = $TaskName
2253
TaskPath = $TaskPath
2354
Ensure = "Absent"
2455
TriggerType = "Unknown"
2556
}
26-
} else {
57+
}
58+
else
59+
{
2760
$action = $task.Actions | Select -First 1
2861
$trigger = $task.Triggers | Select -First 1
2962
$repetition = $trigger.Repetition
3063
$returnScheduleType = "Unknown"
3164
$returnInveral = 0
32-
if ($repetition.Duration -eq $null -and $repetition.Interval -eq $null) {
65+
if ($repetition.Duration -eq $null -and $repetition.Interval -eq $null)
66+
{
3367
$returnScheduleType = "Daily"
3468
$returnInveral = $trigger.DaysInterval
3569
}
36-
if ($repetition.Duration -eq $null -and $repetition.Interval -like "P*D") {
70+
if ($repetition.Duration -eq $null -and $repetition.Interval -like "P*D")
71+
{
3772
$returnScheduleType = "Daily"
3873
[System.Uint32]$returnInveral = $repetition.Interval -replace "P" -replace "D"
3974
}
40-
if (($repetition.Duration -eq "P1D" -or $repetition.Duration -eq $null) -and $repetition.Interval -like "PT*H") {
75+
if (($repetition.Duration -eq "P1D" -or $repetition.Duration -eq $null) `
76+
-and $repetition.Interval -like "PT*H")
77+
{
4178
$returnScheduleType = "Hourly"
4279
[System.Uint32]$returnInveral = $repetition.Interval -replace "PT" -replace "H"
4380
}
44-
if (($repetition.Duration -eq "P1D" -or $repetition.Duration -eq $null) -and $repetition.Interval -like "PT*M") {
81+
if (($repetition.Duration -eq "P1D" -or $repetition.Duration -eq $null) `
82+
-and $repetition.Interval -like "PT*M")
83+
{
4584
$returnScheduleType = "Minutes"
46-
if ($repetition.Interval.Contains('H')) {
47-
[System.Uint32]$returnInveral = [TimeSpan]::Parse(($repetition.Interval -replace "PT" -replace "H",":" -replace "M")).TotalMinutes
48-
} else {
85+
if ($repetition.Interval.Contains('H'))
86+
{
87+
$timeToParse = ($repetition.Interval -replace "PT" -replace "H",":" -replace "M")
88+
[System.Uint32]$returnInveral = [TimeSpan]::Parse($timeToParse).TotalMinutes
89+
}
90+
else
91+
{
4992
[System.Uint32]$returnInveral = $repetition.Interval -replace "PT" -replace "M"
5093
}
5194
}
@@ -68,80 +111,132 @@ function Set-TargetResource
68111
{
69112
param
70113
(
71-
[Parameter(Mandatory=$true)] [System.String] $TaskName,
72-
[Parameter(Mandatory=$false)] [System.String] $TaskPath = "\",
73-
[Parameter(Mandatory=$true)] [System.String] $ActionExecutable,
74-
[Parameter(Mandatory=$false)] [System.String] $ActionArguments,
75-
[Parameter(Mandatory=$false)] [System.String] $ActionWorkingPath,
76-
[Parameter(Mandatory=$true)] [System.String] [ValidateSet("Minutes", "Hourly", "Daily")] $ScheduleType,
77-
[Parameter(Mandatory=$true)] [System.UInt32] $RepeatInterval,
78-
[Parameter(Mandatory=$false)] [System.String] $StartTime = "12:00 AM",
79-
[Parameter(Mandatory=$false)] [System.String] [ValidateSet("Present","Absent")] $Ensure = "Present",
80-
[Parameter(Mandatory=$false)] [System.Management.Automation.PSCredential] $ExecuteAsCredential
114+
[Parameter(Mandatory=$true)]
115+
[System.String]
116+
$TaskName,
117+
118+
[Parameter(Mandatory=$false)]
119+
[System.String]
120+
$TaskPath = "\",
121+
122+
[Parameter(Mandatory=$true)]
123+
[System.String]
124+
$ActionExecutable,
125+
126+
[Parameter(Mandatory=$false)]
127+
[System.String]
128+
$ActionArguments,
129+
130+
[Parameter(Mandatory=$false)]
131+
[System.String]
132+
$ActionWorkingPath,
133+
134+
[Parameter(Mandatory=$true)]
135+
[System.String]
136+
[ValidateSet("Minutes", "Hourly", "Daily")] $ScheduleType,
137+
138+
[Parameter(Mandatory=$true)]
139+
[System.UInt32]
140+
$RepeatInterval,
141+
142+
[Parameter(Mandatory=$false)]
143+
[System.String]
144+
$StartTime = "12:00 AM",
145+
146+
[Parameter(Mandatory=$false)]
147+
[System.String]
148+
[ValidateSet("Present","Absent")]
149+
$Ensure = "Present",
150+
151+
[Parameter(Mandatory=$false)]
152+
[System.Management.Automation.PSCredential]
153+
$ExecuteAsCredential
81154
)
82155

83156
$currentValues = Get-TargetResource @PSBoundParameters
84157

85-
if ($Ensure -eq "Present") {
158+
if ($Ensure -eq "Present")
159+
{
86160
$actionArgs = @{
87161
Execute = $ActionExecutable
88162
}
89-
if ($PSBoundParameters.ContainsKey("ActionArguments")) { $actionArgs.Add("Argument", $ActionArguments)}
90-
if ($PSBoundParameters.ContainsKey("ActionWorkingPath")) { $actionArgs.Add("WorkingDirectory", $ActionWorkingPath)}
163+
if ($PSBoundParameters.ContainsKey("ActionArguments"))
164+
{
165+
$actionArgs.Add("Argument", $ActionArguments)
166+
}
167+
if ($PSBoundParameters.ContainsKey("ActionWorkingPath"))
168+
{
169+
$actionArgs.Add("WorkingDirectory", $ActionWorkingPath)
170+
}
91171
$action = New-ScheduledTaskAction @actionArgs
92172

93173
$date = (Get-Date).Date
94174
$startTime = [DateTime]::Parse("$($date.ToShortDateString()) $StartTime")
95-
switch ($ScheduleType) {
96-
"Minutes" {
175+
switch ($ScheduleType)
176+
{
177+
"Minutes"
178+
{
97179
$repeatAt = New-TimeSpan -Minutes $RepeatInterval
98180
}
99-
"Hourly" {
181+
"Hourly"
182+
{
100183
$repeatAt = New-TimeSpan -Hours $RepeatInterval
101184
}
102-
"Daily" {
185+
"Daily"
186+
{
103187
$repeatAt = New-TimeSpan -Days $RepeatInterval
104188
}
105189
}
106-
$trigger = New-ScheduledTaskTrigger -Once -At $startTime -RepetitionInterval $repeatAt -RepetitionDuration ([TimeSpan]::MaxValue)
190+
$trigger = New-ScheduledTaskTrigger -Once -At $startTime `
191+
-RepetitionInterval $repeatAt `
192+
-RepetitionDuration ([TimeSpan]::MaxValue)
107193

108-
if ($currentValues.Ensure -eq "Absent") {
109-
Write-Verbose "Creating new scheduled task `"$TaskName`""
194+
if ($currentValues.Ensure -eq "Absent")
195+
{
196+
Write-Verbose -Message "Creating new scheduled task `"$TaskName`""
110197

111198
$scheduledTask = New-ScheduledTask -Action $action -Trigger $trigger
112199
$registerArgs = @{
113200
TaskName = $TaskName
114201
TaskPath = $TaskPath
115202
InputObject = $scheduledTask
116203
}
117-
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true) {
204+
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true)
205+
{
118206
$registerArgs.Add("User", $ExecuteAsCredential.UserName)
119207
$registerArgs.Add("Password", $ExecuteAsCredential.GetNetworkCredential().Password)
120-
} else {
208+
}
209+
else
210+
{
121211
$registerArgs.Add("User", "NT AUTHORITY\SYSTEM")
122212
}
123213
Register-ScheduledTask @registerArgs
124214
}
125-
if ($currentValues.Ensure -eq "Present") {
126-
Write-Verbose "Updating scheduled task `"$TaskName`""
215+
if ($currentValues.Ensure -eq "Present")
216+
{
217+
Write-Verbose -Message "Updating scheduled task `"$TaskName`""
127218

128219
$setArgs = @{
129220
TaskName = $TaskName
130221
TaskPath = $TaskPath
131222
Action= $action
132223
Trigger = $trigger
133224
}
134-
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true) {
225+
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true)
226+
{
135227
$setArgs.Add("User", $ExecuteAsCredential.UserName)
136228
$setArgs.Add("Password", $ExecuteAsCredential.GetNetworkCredential().Password)
137-
} else {
229+
}
230+
else
231+
{
138232
$setArgs.Add("User", "NT AUTHORITY\SYSTEM")
139233
}
140234
Set-ScheduledTask @setArgs
141235
}
142236
}
143237

144-
if ($Ensure -eq "Absent") {
238+
if ($Ensure -eq "Absent")
239+
{
145240
Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$false
146241
}
147242
}
@@ -151,35 +246,96 @@ function Test-TargetResource
151246
[OutputType([System.Boolean])]
152247
param
153248
(
154-
[Parameter(Mandatory=$true)] [System.String] $TaskName,
155-
[Parameter(Mandatory=$false)] [System.String] $TaskPath = "\",
156-
[Parameter(Mandatory=$true)] [System.String] $ActionExecutable,
157-
[Parameter(Mandatory=$false)] [System.String] $ActionArguments,
158-
[Parameter(Mandatory=$false)] [System.String] $ActionWorkingPath,
159-
[Parameter(Mandatory=$true)] [System.String] [ValidateSet("Minutes", "Hourly", "Daily")] $ScheduleType,
160-
[Parameter(Mandatory=$true)] [System.UInt32] $RepeatInterval,
161-
[Parameter(Mandatory=$false)] [System.String] $StartTime = "12:00 AM",
162-
[Parameter(Mandatory=$false)] [System.String] [ValidateSet("Present","Absent")] $Ensure = "Present",
163-
[Parameter(Mandatory=$false)] [System.Management.Automation.PSCredential] $ExecuteAsCredential
249+
[Parameter(Mandatory=$true)]
250+
[System.String]
251+
$TaskName,
252+
253+
[Parameter(Mandatory=$false)]
254+
[System.String]
255+
$TaskPath = "\",
256+
257+
[Parameter(Mandatory=$true)]
258+
[System.String]
259+
$ActionExecutable,
260+
261+
[Parameter(Mandatory=$false)]
262+
[System.String]
263+
$ActionArguments,
264+
265+
[Parameter(Mandatory=$false)]
266+
[System.String]
267+
$ActionWorkingPath,
268+
269+
[Parameter(Mandatory=$true)]
270+
[System.String]
271+
[ValidateSet("Minutes", "Hourly", "Daily")] $ScheduleType,
272+
273+
[Parameter(Mandatory=$true)]
274+
[System.UInt32]
275+
$RepeatInterval,
276+
277+
[Parameter(Mandatory=$false)]
278+
[System.String]
279+
$StartTime = "12:00 AM",
280+
281+
[Parameter(Mandatory=$false)]
282+
[System.String]
283+
[ValidateSet("Present","Absent")]
284+
$Ensure = "Present",
285+
286+
[Parameter(Mandatory=$false)]
287+
[System.Management.Automation.PSCredential]
288+
$ExecuteAsCredential
164289
)
165290

166291
$currentValues = Get-TargetResource @PSBoundParameters
167-
if ($Ensure -ne $currentValues.Ensure) { return $false }
168-
if ($Ensure -eq "Present") {
169-
if ($TaskPath -ne $currentValues.TaskPath) { return $false }
170-
if ($ActionExecutable -ne $currentValues.ActionExecutable) { return $false }
171-
if (($PSBoundParameters.ContainsKey("ActionArguments") -eq $true) -and ($ActionArguments -ne $currentValues.ActionArguments)) { return $false }
172-
if (($PSBoundParameters.ContainsKey("ActionWorkingPath") -eq $true) -and ($ActionWorkingPath -ne $currentValues.ActionWorkingPath)) { return $false }
173-
if ($ScheduleType -ne $currentValues.ScheduleType) { return $false }
174-
if ($RepeatInterval -ne $currentValues.RepeatInterval) { return $false }
175-
176-
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true) {
177-
if ($ExecuteAsCredential.UserName.Contains('\') -eq $true) {
292+
if ($Ensure -ne $currentValues.Ensure)
293+
{
294+
return $false
295+
}
296+
if ($Ensure -eq "Present")
297+
{
298+
if ($TaskPath -ne $currentValues.TaskPath)
299+
{
300+
return $false
301+
}
302+
if ($ActionExecutable -ne $currentValues.ActionExecutable)
303+
{
304+
return $false
305+
}
306+
if (($PSBoundParameters.ContainsKey("ActionArguments") -eq $true) `
307+
-and ($ActionArguments -ne $currentValues.ActionArguments))
308+
{
309+
return $false
310+
}
311+
if (($PSBoundParameters.ContainsKey("ActionWorkingPath") -eq $true) `
312+
-and ($ActionWorkingPath -ne $currentValues.ActionWorkingPath))
313+
{
314+
return $false
315+
}
316+
if ($ScheduleType -ne $currentValues.ScheduleType)
317+
{
318+
return $false
319+
}
320+
if ($RepeatInterval -ne $currentValues.RepeatInterval)
321+
{
322+
return $false
323+
}
324+
325+
if ($PSBoundParameters.ContainsKey("ExecuteAsCredential") -eq $true)
326+
{
327+
if ($ExecuteAsCredential.UserName.Contains('\') -eq $true)
328+
{
178329
$localUser = $ExecuteAsCredential.UserName.Split('\')[1]
179-
} else {
330+
}
331+
else
332+
{
180333
$localUser = $ExecuteAsCredential.UserName
181334
}
182-
if ($localUser -ne $currentValues.ExecuteAsCredential) { return $false }
335+
if ($localUser -ne $currentValues.ExecuteAsCredential)
336+
{
337+
return $false
338+
}
183339
}
184340
}
185341

0 commit comments

Comments
 (0)