DSCResources/xScheduledTask/xScheduledTask.psm1
Import-Module posh-kentico enum Ensure { Absent Present } [DscResource()] class ScheduledTaskResource { [DscProperty(Key)] [string] $Name; [DscProperty()] [string] $AssemblyName; [DscProperty()] [string] $ClassName; [DscProperty()] [string] $TaskData; [DscProperty()] [string] $DisplayName; [DscProperty(Mandatory)] [Ensure] $Ensure; [DscProperty()] [CMS.Scheduler.TaskInterval] $Interval; [DscProperty()] [CMS.SiteProvider.SiteInfo] $Site; [bool] Test() { $scheduledTask = Get-CMSScheduledTask -Name $this.Name $intervalString = [CMS.Scheduler.SchedulingHelper]::EncodeInterval($this.Interval) $this.site = $scheduledTask | Get-CMSSite if ($null -ne $scheduledTask) { return $this.Ensure -eq "Present" -and $this.AssemblyName -eq $scheduledTask.TaskAssemblyName -and $this.ClassName -eq $scheduledTask.TaskClass -and $this.TaskData -eq $scheduledTask.TaskData -and $this.DisplayName -eq $scheduledTask.TaskDisplayName -and $intervalString -eq $scheduledTask.TaskInterval -and $this.Site.SiteID -eq $scheduledTask.TaskSiteID } else { return $this.Ensure -eq "Absent" } } [void] Set() { if ([string]::IsNullOrEmpty($this.Ensure)) { $this.Ensure = "Present" } $scheduledTask = Get-CMSScheduledTask -Name $this.Name if ($this.Ensure -eq "Present") { if ($null -ne $scheduledTask) { $scheduledTask.TaskAssemblyName = $this.AssemblyName $scheduledTask.TaskClass = $this.ClassName $scheduledTask.TaskData = $this.TaskData $scheduledTask.TaskDisplayName = $this.DisplayName if ($null -ne $this.Site) { $scheduledTask.TaskSiteID = $this.Site.SiteID } $scheduledTask | Set-CMSScheduledTask -Interval $this.Interval } else { if ($null -ne $this.Site) { New-CMSScheduledTask -AssemblyName $this.AssemblyName -Class $this.ClassName -Data $this.TaskData -DisplayName $this.DisplayName -Interval $this.Interval -Site $this.Site } else { New-CMSScheduledTask -AssemblyName $this.AssemblyName -Class $this.ClassName -Data $this.TaskData -DisplayName $this.DisplayName -Interval $this.Interval } } } elseif ($this.Ensure -eq "Absent") { if ($null -ne $scheduledTask) { $scheduledTask | Remove-CMSScheduledTask -Confirm:$false } } } [ScheduledTaskResource] Get() { $scheduledTask = Get-CMSScheduledTask -Name $this.Name if ($null -ne $scheduledTask) { $this.Ensure = "Present" } else { $this.Ensure = "Absent" } $this.AssemblyName = $scheduledTask.TaskAssemblyName $this.ClassName = $scheduledTask.ClassName $this.TaskData = $scheduledTask.TaskData $this.DisplayName = $scheduledTask.TaskDisplayName $this.Interval = $scheduledTask | Get-CMSScheduledTaskInterval $this.Site = $scheduledTask | Get-CMSSite return $this } } |