1- Function Get-TargetResource {
2- param (
3- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$ExecutablePath ,
4- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$Params ,
5- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$Name ,
6- [ValidateSet (" MINUTE" , " HOURLY" , " DAILY" , " WEEKLY" , " ONSTART" , " ONLOGON" )][string ]$IntervalModifier = " MINUTE" ,
7- [ValidateSet (" PRESENT" , " ABSENT" )][string ]$Ensure = " PRESENT" ,
8- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][int ]$Interval = 5
9- )
10- @ {
11- ExecutablePath = $ExecutablePath ;
12- Params = $Params ;
13- Name = $Name ;
14- IntervalModifier = $IntervalModifier ;
15- Ensure = $Ensure ;
16- Interval = $Interval ;
1+ # Converts CimInstance array of type KeyValuePair to hashtable
2+ function Convert-KeyValuePairArrayToHashtable
3+ {
4+ param (
5+ [parameter (Mandatory = $true )]
6+ [Microsoft.Management.Infrastructure.CimInstance []]
7+ $array
8+ )
9+
10+ $SwitchList = @ (
11+ " Once" ,
12+ " Weekly" ,
13+ " AsJob" ,
14+ " Daily" ,
15+ " AtLogOn" ,
16+ " AtStartup" ,
17+ " AllowStartIfOnBatteries" ,
18+ " Disable" ,
19+ " DisallowDemandStart" ,
20+ " DisallowHardTerminate"
21+ " DisallowStartOnRemoteAppSession"
22+ " DontStopIfGoingOnBatteries"
23+ " DontStopOnIdleEnd" ,
24+ " Hidden" ,
25+ " MaintenanceExclusive" ,
26+ " RestartOnIdle" ,
27+ " RunOnlyIfIdle" ,
28+ " RunOnlyIfNetworkAvailable" ,
29+ " StartWhenAvailable" ,
30+ " WakeToRun"
31+ )
32+ try
33+ {
34+ $hashtable = @ {}
35+ foreach ($item in $array )
36+ {
37+ # Fix switch parameter values to help cmdlets handle them better
38+ if ($SwitchList -contains $item.key )
39+ {
40+ $hashtable += @ {$item.Key = ([bool ]::Parse($item.Value ))}
41+ }
42+ else
43+ {
44+ $hashtable += @ {$item.Key = $item.Value }
45+ }
1746 }
47+ return $hashtable
48+ }
49+ catch
50+ {
51+ Write-Verbose " Failed to convert variable. `n $_ .Exception.Message"
1852 }
53+ }
54+
55+ Function Get-TargetResource
56+ {
57+ [CmdletBinding ()]
58+ [OutputType ([System.Collections.Hashtable ])]
59+ param (
60+ [Parameter (Mandatory = $true )]
61+ [ValidateNotNullOrEmpty ()]
62+ [string ]$Name ,
63+
64+ [ValidateSet (" Present" , " Absent" )]
65+ [string ]$Ensure ,
66+
67+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()]
68+ [Microsoft.Management.Infrastructure.CimInstance []]$ActionParams ,
1969
20- Function Test-TargetResource {
21- param (
22- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$ExecutablePath ,
23- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$Params ,
24- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$Name ,
25- [ValidateSet (" MINUTE" , " HOURLY" , " DAILY" , " WEEKLY" , " ONSTART" , " ONLOGON" )][string ]$IntervalModifier = " MINUTE" ,
26- [ValidateSet (" PRESENT" , " ABSENT" )][string ]$Ensure = " PRESENT" ,
27- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][int ]$Interval = 5
28- )
29- $tasks = Get-ScheduledTask
70+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()]
71+ [Microsoft.Management.Infrastructure.CimInstance []]$TriggerParams ,
3072
31- if ($tasks.TaskName -contains $Name ) {
32- return $true
73+ [Parameter (Mandatory = $false )]
74+ [Microsoft.Management.Infrastructure.CimInstance []]$TaskSettings
75+ )
76+
77+ try
78+ {
79+ $tasks = Get-ScheduledTask
80+ if ($tasks.TaskName -contains $Name )
81+ {
82+ $Ensure = " Present"
83+ $task = Get-ScheduledTask - TaskName $Name
84+ $Action = $task.Actions
85+ $Trigger = $task.Triggers
86+ # $Settings = $task.Settings
3387 }
34- else {
35- return $false
88+ else
89+ {
90+ $Ensure = " Absent"
91+ $Action = $ActionParams
92+ $Trigger = $TriggerParams
93+ # $Settings = $TaskSettings
3694 }
95+ }
96+ catch
97+ {
98+ Write-Verbose " $_ .Exception.Message"
99+ }
37100
101+ @ {
102+ Name = $Name ;
103+ ActionParams = $Action ;
104+ TriggerParams = $Trigger ;
105+ # TaskSettings = $Settings;
106+ Ensure = $Ensure ;
38107 }
108+ }
39109
40- Function Set-TargetResource {
41- param (
42- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$ExecutablePath ,
43- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$Params ,
44- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][string ]$Name ,
45- [ValidateSet (" MINUTE" , " HOURLY" , " DAILY" , " WEEKLY" , " ONSTART" , " ONLOGON" )][string ]$IntervalModifier = " MINUTE" ,
46- [ValidateSet (" PRESENT" , " ABSENT" )][string ]$Ensure = " PRESENT" ,
47- [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()][int ]$Interval = 5
48- )
49- $tasks = Get-ScheduledTask
110+ Function Test-TargetResource
111+ {
112+ [CmdletBinding ()]
113+ [OutputType ([Boolean ])]
114+ param (
115+ [Parameter (Mandatory = $true )]
116+ [ValidateNotNullOrEmpty ()]
117+ [string ]$Name ,
118+
119+ [ValidateSet (" Present" , " Absent" )]
120+ [string ]$Ensure = " Present" ,
121+
122+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()]
123+ [Microsoft.Management.Infrastructure.CimInstance []]$ActionParams ,
50124
51- if ($Ensure -eq " ABSENT" ) {
52- if ($tasks.TaskName -contains $Name ) {
53- Write-Verbose " Deleting Scheduled Task $Name "
54- try {
55- schtasks.exe / delete / tn $Name / f
56- }
57- catch {
58- Write-EventLog - LogName DevOps - Source RS_rsScheduledTask - EntryType Error - EventId 1002 - Message " Failed to delete scheduled task $Name `n $_ .Exception.Message"
59- }
125+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()]
126+ [Microsoft.Management.Infrastructure.CimInstance []]$TriggerParams ,
127+
128+ [Parameter (Mandatory = $false )]
129+ [Microsoft.Management.Infrastructure.CimInstance []]$TaskSettings
130+ )
131+
132+ if ($ (Get-ScheduledTask ).TaskName -contains $Name )
133+ {
134+ Write-Verbose " Task `" $Name `" is present"
135+ $TaskPresent = $true
136+ }
137+ else
138+ {
139+ Write-Verbose " Task `" $Name `" was not found"
140+ $TaskPresent = $false
141+ }
142+
143+ if ($TaskPresent -and $Ensure -eq " Present" )
144+ {
145+ Write-Verbose " Task `" $Name `" is present and in line with Ensure setting of `" $Ensure `" "
146+ return $true
147+ }
148+
149+ if (! $TaskPresent -and $Ensure -eq " Absent" )
150+ {
151+ Write-Verbose " Task `" $Name `" is absent and in line with Ensure setting of `" $Ensure `" "
152+ return $true
153+ }
154+ Write-Verbose " Current state is not in line with Ensure setting: `" $Ensure `" "
155+ return $false
156+ }
157+
158+ Function Set-TargetResource
159+ {
160+ [CmdletBinding ()]
161+ param (
162+ [Parameter (Mandatory = $true )]
163+ [ValidateNotNullOrEmpty ()]
164+ [string ]$Name ,
165+
166+ [ValidateSet (" Present" , " Absent" )]
167+ [string ]$Ensure = " Present" ,
168+
169+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()]
170+ [Microsoft.Management.Infrastructure.CimInstance []]
171+ $ActionParams ,
172+
173+ [Parameter (Mandatory = $true )][ValidateNotNullOrEmpty ()]
174+ [Microsoft.Management.Infrastructure.CimInstance []]
175+ $TriggerParams ,
176+
177+ [Parameter (Mandatory = $false )]
178+ [Microsoft.Management.Infrastructure.CimInstance []]
179+ $TaskSettings
180+ )
181+
182+ $tasks = Get-ScheduledTask
183+
184+ if ($Ensure -eq " Absent" )
185+ {
186+ if ($tasks.TaskName -contains $Name )
187+ {
188+ Write-Verbose " Deleting Scheduled Task $Name "
189+ try
190+ {
191+ Unregister-ScheduledTask - TaskName $Name - Confirm:$false
192+ }
193+ catch
194+ {
195+ Write-Verbose " Failed to delete scheduled task $Name `n $_ .Exception.Message"
196+ # Write-EventLog -LogName DevOps -Source RS_rsScheduledTask -EntryType Error -EventId 1002 -Message "Failed to delete scheduled task $Name `n $_.Exception.Message"
60197 }
61198 }
199+ }
62200
63- if ($Ensure -eq " PRESENT" ) {
64- if ($tasks -notcontains $Name ) {
65- Write-Verbose " Creating New Scheduled Task $Name $ExecutablePath $Params "
66- try {
67- schtasks.exe / create / tn $Name / tr $ ($ExecutablePath , $Params -join ' ' ) / sc $IntervalModifier / mo $Interval / ru system / f
201+ if ($Ensure -eq " Present" )
202+ {
203+ try
204+ {
205+ # Check if this task is already in place and overwrite it with the current settings by removing it first
206+ if ($tasks.TaskName -contains $Name )
207+ {
208+ Write-Verbose " Deleting existing Scheduled Task $Name "
209+ Unregister-ScheduledTask - TaskName $Name - Confirm:$false
210+ }
211+
212+ Write-Verbose " Creating New Scheduled Task $Name "
213+
214+ # Convert all KeyValue Pair parameter sets from ciminstance to hash table
215+ [hashtable ]$ActionParams = Convert-KeyValuePairArrayToHashtable - array $ActionParams
216+ [hashtable ]$TriggerParams = Convert-KeyValuePairArrayToHashtable - array $TriggerParams
217+ if ($TaskSettings -ne $null )
218+ {
219+ [hashtable ]$TaskSettings = Convert-KeyValuePairArrayToHashtable - array $TaskSettings
220+ }
221+
222+ # Ensure for mandatory parameter
223+ if ($ActionParams.Execute -ne $null )
224+ {
225+ Write-Verbose " Creating the $Name Scheduled Task Action..."
226+ $Action = New-ScheduledTaskAction @ActionParams
227+ }
228+ else
229+ {
230+ Throw ' Mandatory "ExecutablePath" parameter is not defined in $ActionParams'
231+ }
232+
233+ # Validate parameter sets for New-ScheduledTaskTrigger cmdlet
234+ if (($TriggerParams.Once ) -or ($TriggerParams.Daily ) -or ($TriggerParams.Weekly ))
235+ {
236+ if ($TriggerParams.At -eq $null )
237+ {
238+ Write-Verbose " Mandatory At parameter not provided, assigning current date and time"
239+ $TriggerParams.At = Get-Date
68240 }
69- catch {
70- Write-EventLog - LogName DevOps - Source RS_rsScheduledTask - EntryType Information - EventId 1000 - Message " Failed to create scheduled task $Name `n $_ .Exception.Message"
241+
242+ if (($TriggerParams.Weekly -ne $null ) -and ($TriggerParams.DaysOfWeek -eq $null ))
243+ {
244+ Throw ' $TriggerParams is missing the mandatory "DaysOfWeek" parameter from the Weekly parameter set'
71245 }
72- }
73- }
74246
247+ if ($TriggerParams.Once )
248+ {
249+ if ($TriggerParams.RepetitionInterval -eq $null )
250+ {
251+ Throw ' $TriggerParams is missing the mandatory "RepetitionInterval" parameter from the Once parameter set'
252+ }
253+
254+ if ($TriggerParams.RepetitionDuration -eq $null )
255+ {
256+ Write-Verbose ' RepetitionDuration not defined, so seting to indefinite...'
257+ $TriggerParams.RepetitionDuration = ([timeSpan ]::maxvalue)
258+ }
259+ }
260+ }
261+ $Trigger = New-ScheduledTaskTrigger @TriggerParams
262+
263+ if ($TaskSettings -ne $null )
264+ {
265+ $Settings = New-ScheduledTaskSettingsSet @TaskSettings
266+ }
267+ else
268+ {
269+ Write-Verbose ' TaskSettings parameter set is not defined, applying defaults'
270+ $Settings = New-ScheduledTaskSettingsSet
271+ }
272+
273+ # Currently defaulting to SYSTEM account for now
274+ Register-ScheduledTask - TaskName $Name - Action $Action - Trigger $Trigger - Settings $Settings - User " SYSTEM"
275+ }
276+ catch
277+ {
278+ Write-Verbose " Failed to create scheduled task $Name `n $_ "
279+ # Write-EventLog -LogName DevOps -Source RS_rsScheduledTask -EntryType Information -EventId 1000 -Message "Failed to create scheduled task $Name `n $_.Exception.Message"
280+ }
75281 }
282+ }
283+
76284Export-ModuleMember - Function *- TargetResource
0 commit comments