DscResources/DscLcmMaintenanceWindows/DscLcmMaintenanceWindows.schema.psm1

configuration DscLcmMaintenanceWindows {
    Param (
        [Parameter(Mandatory)]
        [hashtable[]]$MaintenanceWindow
    )

    Import-DscResource -ModuleName xPSDesiredStateConfiguration
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    $on = '1st', '2nd', '3rd', '4th', 'last'
    $daysOfWeek = [System.Enum]::GetNames([System.DayOfWeek])

    foreach ($window in $MaintenanceWindow.GetEnumerator()) {
        
        if ($window.DayOfWeek) {
            if ($window.DayOfWeek -notin $daysOfWeek) {
                Write-Error "DayOfWeek '$($window.DayOfWeek)' of maintenance window '$($window.Name)' is not in the supported range ('$($daysOfWeek -join ', ')')."
            }
        }

        if ($window.On) {
            if ($window.On -notin $on) {
                Write-Error "Property 'On' set to '$($window.On)' of maintenance window '$($window.Name)' is not in the supported range ('$($on -join ', ')')."
            }
        }
    }

    Script MaintenanceWindowsCheck {
        TestScript = {
            try {
                $existingWindows = Get-ChildItem -Path HKLM:\SOFTWARE\DscLcmController\MaintenanceWindows -ErrorAction Stop | Select-Object -ExpandProperty PSChildName
                $diff = Compare-Object -ReferenceObject $existingWindows -DifferenceObject $using:MaintenanceWindow.Name
                [bool]-not $diff
        
            }
            catch {
                $false
            }
        }
        SetScript = {
            Write-Verbose 'There is a difference in the maintainance window definition. Removing currently configured maintenance windows.'
            Remove-Item -Path HKLM:\SOFTWARE\DscLcmController\MaintenanceWindows -Force -Recurse -ErrorAction SilentlyContinue
        }

        GetScript = {
            @{
                Result = Get-ChildItem -Path HKLM:\SOFTWARE\DscLcmController\MaintenanceWindows -ErrorAction SilentlyContinue | Select-Object -ExpandProperty PSChildName
            }
        }
    }

    foreach ($window in $MaintenanceWindow.GetEnumerator()) {

        xRegistry "StartTime_$($window.Name)" {
            Key       = "HKEY_LOCAL_MACHINE\SOFTWARE\DscLcmController\MaintenanceWindows\$($window.Name)"
            ValueName = 'StartTime'
            ValueData = $window.StartTime
            ValueType = 'String'
            Ensure    = 'Present'
            Force     = $true
        }

        xRegistry "Timespan_$($window.Name)" {
            Key       = "HKEY_LOCAL_MACHINE\SOFTWARE\DscLcmController\MaintenanceWindows\$($window.Name)"
            ValueName = 'Timespan'
            ValueData = $window.Timespan
            ValueType = 'String'
            Ensure    = 'Present'
            Force     = $true
        }

        xRegistry "DayOfWeek_$($window.Name)" {
            Key       = "HKEY_LOCAL_MACHINE\SOFTWARE\DscLcmController\MaintenanceWindows\$($window.Name)"
            ValueName = 'DayOfWeek'
            ValueData = $window.DayOfWeek
            ValueType = 'String'
            Ensure    = 'Present'
            Force     = $true
        }

        xRegistry "On_$($window.Name)" {
            Key       = "HKEY_LOCAL_MACHINE\SOFTWARE\DscLcmController\MaintenanceWindows\$($window.Name)"
            ValueName = 'On'
            ValueData = $window.On
            ValueType = 'String'
            Ensure    = 'Present'
            Force     = $true
        }
    }
}