Modules/SharePointDsc.Search/SPSearchContentSource.Schedules.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function Get-SPDSCSearchCrawlSchedule
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param(
        [Parameter()]
        $Schedule
    )

    if ($null -eq $Schedule)
    {
        return @{
            ScheduleType = "None"
        }
    }

    $scheduleType = $Schedule.GetType().Name
    $result = @{
        CrawlScheduleRepeatDuration = $Schedule.RepeatDuration
        CrawlScheduleRepeatInterval = $Schedule.RepeatInterval
        StartHour = $Schedule.StartHour
        StartMinute = $Schedule.StartMinute
    }

    switch ($scheduleType)
    {
        "DailySchedule" {
            $result.Add("ScheduleType", "Daily")
            $result.Add("CrawlScheduleRunEveryInterval", $Schedule.DaysInterval)
        }
        "WeeklySchedule" {
            $result.Add("ScheduleType", "Weekly")
            $result.Add("CrawlScheduleRunEveryInterval", $Schedule.WeeksInterval)
            $result.Add("CrawlScheduleDaysOfWeek", $Schedule.DaysOfWeek)
        }
        "MonthlyDateSchedule" {
            $result.Add("ScheduleType", "Monthly")
            $result.Add("CrawlScheduleDaysOfMonth", ($Schedule.DaysOfMonth.ToString() -replace "Day"))
            $result.Add("CrawlScheduleMonthsOfYear", $schedule.MonthsOfYear)
        }
        Default {
            throw "An unknown schedule type was detected"
        }
    }
    return $result
}

function Test-SPDSCSearchCrawlSchedule
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param(
        [Parameter(Mandatory = $true)] $CurrentSchedule,
        [Parameter(Mandatory = $true)] $DesiredSchedule
    )

    Import-Module -Name (Join-Path -Path $PSScriptRoot `
                                   -ChildPath "..\SharePointDsc.Util\SharePointDsc.Util.psm1")

    if ($CurrentSchedule.ScheduleType -ne $DesiredSchedule.ScheduleType)
    {
        return $false
    }

    if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                     -PropertyName "CrawlScheduleRepeatDuration") -eq $true `
        -and $CurrentSchedule.CrawlScheduleRepeatDuration -ne $DesiredSchedule.CrawlScheduleRepeatDuration)
    {
        return $false
    }
    if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                     -PropertyName "CrawlScheduleRepeatInterval") -eq $true `
        -and $CurrentSchedule.CrawlScheduleRepeatInterval -ne $DesiredSchedule.CrawlScheduleRepeatInterval)
    {
        return $false
    }
    if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                     -PropertyName "StartHour") -eq $true `
        -and $CurrentSchedule.StartHour -ne $DesiredSchedule.StartHour)
    {
        return $false
    }
    if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                     -PropertyName "StartMinute") -eq $true `
        -and $CurrentSchedule.StartMinute -ne $DesiredSchedule.StartMinute)
    {
        return $false
    }

    switch ($CurrentSchedule.ScheduleType)
    {
        "Daily" {
            if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                             -PropertyName "CrawlScheduleRunEveryInterval") -eq $true `
                -and $CurrentSchedule.CrawlScheduleRunEveryInterval -ne $DesiredSchedule.CrawlScheduleRunEveryInterval)
            {
                return $false
            }
        }
        "Weekly" {
            if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                             -PropertyName "CrawlScheduleRunEveryInterval") -eq $true `
                -and $CurrentSchedule.CrawlScheduleRunEveryInterval -ne $DesiredSchedule.CrawlScheduleRunEveryInterval)
            {
                return $false
            }
            $days = $CurrentSchedule.CrawlScheduleDaysOfWeek.ToString()
            if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                             -PropertyName "CrawlScheduleDaysOfWeek") -eq $true `
                -and $null -ne (Compare-Object -ReferenceObject $days.Split(', ', [System.StringSplitOptions]::RemoveEmptyEntries) `
                                               -DifferenceObject $DesiredSchedule.CrawlScheduleDaysOfWeek))
            {
                return $false
            }
        }
        "Monthly" {
            if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                             -PropertyName "CrawlScheduleDaysOfMonth") -eq $true `
                -and $CurrentSchedule.CrawlScheduleDaysOfMonth -ne $DesiredSchedule.CrawlScheduleDaysOfMonth)
            {
                return $false
            }
            $months = $CurrentSchedule.CrawlScheduleMonthsOfYear.ToString()
            if ((Test-SPDSCObjectHasProperty -Object $DesiredSchedule `
                                             -PropertyName "CrawlScheduleMonthsOfYear") -eq $true `
                -and $null -eq (Compare-Object -ReferenceObject $months.Split(', ', [System.StringSplitOptions]::RemoveEmptyEntries) `
                                               -DifferenceObject $DesiredSchedule.CrawlScheduleMonthsOfYear))
            {
                return $false
            }
        }
    }
    return $true
}

Export-ModuleMember -Function *