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 ;
17+ }
18+ }
19+
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
30+
31+ if ($tasks -contains $Name ) {
32+ return $true
33+ }
34+ else {
35+ return $false
36+ }
37+
38+ }
39+
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
50+
51+ if ($Ensure -eq " ABSENT" ) {
52+ if ($tasks -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+ }
60+ }
61+ }
62+
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
68+ }
69+ catch {
70+ Write-EventLog - LogName DevOps - Source RS_rsScheduledTask - EntryType Information - EventId 1000 - Message " Failed to create scheduled task $Name `n $_ .Exception.Message"
71+ }
72+ }
73+ }
74+
75+ }
76+ Export-ModuleMember - Function *- TargetResource
0 commit comments