Modules/SharePointDsc.WebApplication/SPWebApplication.Throttling.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function Get-SPDSCWebApplicationThrottlingConfig 
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param(
        [parameter(Mandatory = $true)] 
        $WebApplication
    )
    return @{
        ListViewThreshold        = $WebApplication.MaxItemsPerThrottledOperation
        AllowObjectModelOverride = $WebApplication.AllowOMCodeOverrideThrottleSettings
        AdminThreshold           = $WebApplication.MaxItemsPerThrottledOperationOverride
        ListViewLookupThreshold  = $WebApplication.MaxQueryLookupFields
        HappyHourEnabled         = $WebApplication.UnthrottledPrivilegedOperationWindowEnabled
        HappyHour = @{
            Hour     = $WebApplication.DailyStartUnthrottledPrivilegedOperationsHour
            Minute   = $WebApplication.DailyStartUnthrottledPrivilegedOperationsMinute
            Duration = $WebApplication.DailyUnthrottledPrivilegedOperationsDuration
        }
        UniquePermissionThreshold = $WebApplication.MaxUniquePermScopesPerList
        RequestThrottling         = $WebApplication.HttpThrottleSettings.PerformThrottle
        ChangeLogEnabled          = $WebApplication.ChangeLogExpirationEnabled
        ChangeLogExpiryDays       = $WebApplication.ChangeLogRetentionPeriod.Days
        EventHandlersEnabled      = $WebApplication.EventHandlersEnabled
    }
}

function Set-SPDSCWebApplicationThrottlingConfig 
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)] 
        $WebApplication,

        [parameter(Mandatory = $true)] 
        $Settings
    )

    # Format here is SPWebApplication property = Custom settings property
    $mapping = @{
        MaxItemsPerThrottledOperation               = "ListViewThreshold"
        AllowOMCodeOverrideThrottleSettings         = "AllowObjectModelOverride"
        MaxItemsPerThrottledOperationOverride       = "AdminThreshold"
        MaxQueryLookupFields                        = "ListViewLookupThreshold"
        UnthrottledPrivilegedOperationWindowEnabled = "HappyHourEnabled"
        MaxUniquePermScopesPerList                  = "UniquePermissionThreshold"
        EventHandlersEnabled                        = "EventHandlersEnabled"
        ChangeLogExpirationEnabled                  = "ChangeLogEnabled"
    } 
    $mapping.Keys | ForEach-Object -Process {
        Set-SPDscObjectPropertyIfValuePresent -ObjectToSet $WebApplication `
                                              -PropertyToSet $_ `
                                              -ParamsValue $settings `
                                              -ParamKey $mapping[$_]
    }

    # Set throttle settings child property seperately
    Set-SPDscObjectPropertyIfValuePresent -ObjectToSet $WebApplication.HttpThrottleSettings `
                                          -PropertyToSet "PerformThrottle" `
                                          -ParamsValue $Settings `
                                          -ParamKey "RequestThrottling"
    
    # Create time span object separately
    if ((Test-SPDSCObjectHasProperty $Settings "ChangeLogExpiryDays") -eq $true) 
    {
        $days = New-TimeSpan -Days $Settings.ChangeLogExpiryDays
        $WebApplication.ChangeLogRetentionPeriod = $days
    }
}


function Set-SPDSCWebApplicationHappyHourConfig 
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)] 
        $WebApplication,
        
        [parameter(Mandatory = $true)] 
        $Settings
    )

    if ((Test-SPDSCObjectHasProperty $Settings "Hour") -eq $false `
      -or (Test-SPDSCObjectHasProperty $Settings "Minute") -eq $false `
      -or (Test-SPDSCObjectHasProperty $Settings "Duration") -eq $false) 
    {
        throw "Happy hour settings must include 'hour', 'minute' and 'duration'"
    } 
    else 
    {
        if ($Settings.Hour -lt 0 -or $Settings.Hour -gt 23) 
        {
            throw "Happy hour setting 'hour' must be between 0 and 23"
        }
        if ($Settings.Minute -lt 0 -or $Settings.Minute -gt 59) 
        {
            throw "Happy hour setting 'minute' must be between 0 and 59"
        }
        if ($Settings.Duration -lt 0 -or $Settings.Duration -gt 23) 
        {
            throw "Happy hour setting 'hour' must be between 0 and 23"
        }
        $h = $Settings.Hour
        $m = $Settings.Minute
        $d = $Settings.Duration
        $WebApplication.SetDailyUnthrottledPrivilegedOperationWindow($h, $m, $d)
    }
}

function Test-SPDSCWebApplicationThrottlingConfig 
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param(
        [parameter(Mandatory = $true)] 
        $CurrentSettings,
        
        [parameter(Mandatory = $true)] 
        $DesiredSettings
    )

    $relPath = "..\..\Modules\SharePointDsc.Util\SharePointDsc.Util.psm1"
    Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath $relPath -Resolve)
    $testReturn = Test-SPDscParameterState -CurrentValues $CurrentSettings `
                                                     -DesiredValues $DesiredSettings `
                                                     -ValuesToCheck @(
                                                         "ListViewThreshold",
                                                         "AllowObjectModelOverride",
                                                         "AdminThreshold",
                                                         "ListViewLookupThreshold",
                                                         "HappyHourEnabled",
                                                         "UniquePermissionThreshold",
                                                         "RequestThrottling",
                                                         "ChangeLogEnabled",
                                                         "ChangeLogExpiryDays",
                                                         "EventHandlersEnabled"
                                                     )
    if ($testReturn -eq $true) 
    {
            if ($null -ne $DesiredSettings.HappyHour)
            {
                $DesiredHappyHour = @{}
                if ($null -ne $DesiredSettings.HappyHour.Hour) 
                {
                    $DesiredHappyHour.Add("Hour",[int32]$DesiredSettings.HappyHour.Hour)
                } 
                else 
                {
                    $DesiredHappyHour.Add("Hour",$null)
                }
                if ($null -ne $DesiredSettings.HappyHour.Minute) 
                {
                    $DesiredHappyHour.Add("Minute",[int32]$DesiredSettings.HappyHour.Minute)
                } else 
                {
                    $DesiredHappyHour.Add("Minute",$null)
                }
                if ($null -ne $DesiredSettings.HappyHour.Duration) 
                {
                    $DesiredHappyHour.Add("Duration",[int32]$DesiredSettings.HappyHour.Duration)
                } else 
                {
                    $DesiredHappyHour.Add("Duration",$null)
                }

            $testReturn = Test-SPDscParameterState -CurrentValues $CurrentSettings.HappyHour `
                                                   -DesiredValues $DesiredHappyHour `
                                                   -ValuesToCheck @("Hour", "Minute", "Duration")
        }
    }
    return $testReturn
}