DSCResources/MSFT_SPSearchIndexPartition/MSFT_SPSearchIndexPartition.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.UInt32]
        $Index,

        [Parameter(Mandatory = $true)]
        [System.String[]]
        $Servers,

        [Parameter()]
        [System.String]
        $RootDirectory,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ServiceAppName,

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $InstallAccount
    )

    Write-Verbose -Message "Getting Search Index Partition '$Index' settings"

    $result = Invoke-SPDSCCommand -Credential $InstallAccount `
                                  -Arguments $PSBoundParameters `
                                  -ScriptBlock {
        $params = $args[0]
        $ConfirmPreference = 'None'

        $ssa = Get-SPEnterpriseSearchServiceApplication -Identity $params.ServiceAppName
        $currentTopology = $ssa.ActiveTopology

        $searchComponent = Get-SPEnterpriseSearchComponent -SearchTopology $currentTopology | `
                                Where-Object -FilterScript {
                                    ($_.GetType().Name -eq "IndexComponent") `
                                    -and ($_.IndexPartitionOrdinal -eq $params.Index)
                                }

        $IndexComponents = $searchComponent.ServerName
        $rootDirectory = $searchComponent.RootDirectory

        if ($rootDirectory -eq "")
        {
            $ssi = Get-SPEnterpriseSearchServiceInstance
            $component = $ssi.Components | Select-Object -First 1
            $rootDirectory = $component.IndexLocation
        }

        return @{
            Index = $params.Index
            Servers = $IndexComponents
            RootDirectory = $rootDirectory
            ServiceAppName = $params.ServiceAppName
            InstallAccount = $params.InstallAccount
        }
    }
    return $result
}

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.UInt32]
        $Index,

        [Parameter(Mandatory = $true)]
        [System.String[]]
        $Servers,

        [Parameter()]
        [System.String]
        $RootDirectory,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ServiceAppName,

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $InstallAccount
    )

    Write-Verbose -Message "Setting Search Index Partition '$Index' settings"

    $CurrentValues = Get-TargetResource @PSBoundParameters

    Invoke-SPDSCCommand -Credential $InstallAccount `
                        -Arguments @($PSBoundParameters, $CurrentValues) `
                        -ScriptBlock {
        $params = $args[0]
        $CurrentValues = $args[1]
        $ConfirmPreference = 'None'

        $AllSearchServers = $params.Servers

        # Ensure the search service instance is running on all servers
        foreach($searchServer in $AllSearchServers)
        {
            $searchService = Get-SPEnterpriseSearchServiceInstance -Identity $searchServer
            if($searchService.Status -eq "Offline")
            {
                Write-Verbose -Message "Start Search Service Instance"
                Start-SPEnterpriseSearchServiceInstance -Identity $searchService
            }

            #Wait for Search Service Instance to come online
            $loopCount = 0
            $online = Get-SPEnterpriseSearchServiceInstance -Identity $searchServer
            do
            {
                $online = Get-SPEnterpriseSearchServiceInstance -Identity $searchServer
                Write-Verbose -Message "Waiting for service: $($online.TypeName)"
                $loopCount++
                Start-Sleep -Seconds 30
            }
            until ($online.Status -eq "Online" -or $loopCount -eq 20)
        }

        if ($params.ContainsKey("RootDirectory") -eq $true)
        {
            # Create the index partition directory on each remote server
            foreach($IndexPartitionServer in $params.Servers)
            {
                $networkPath = "\\$IndexPartitionServer\" + $params.RootDirectory.Replace(":\", "$\")
                New-Item -Path $networkPath -ItemType Directory -Force
            }

            # Create the directory on the local server as it will not apply the topology without it
            if ((Test-Path -Path $params.RootDirectory) -eq $false)
            {
                New-Item $params.RootDirectory -ItemType Directory -Force
            }
        }

        # Get all service service instances to assign topology components to
        $AllSearchServiceInstances = @{}
        foreach ($server in $AllSearchServers)
        {
            $si = Get-SPEnterpriseSearchServiceInstance -Identity $server
            $AllSearchServiceInstances.Add($server, $si)
        }

        # Get current topology and prepare a new one
        $ssa = Get-SPEnterpriseSearchServiceApplication -Identity $params.ServiceAppName
        $currentTopology = $ssa.ActiveTopology
        $newTopology = New-SPEnterpriseSearchTopology -SearchApplication $ssa `
                                                      -Clone `
                                                      -SearchTopology $currentTopology

        $componentTypes = @{
            Servers = "IndexComponent"
        }

        # Build up the topology changes for each object type
        @("Servers") | ForEach-Object -Process {
            $CurrentSearchProperty = $_
            Write-Verbose -Message "Setting components for '$CurrentSearchProperty' property"
            $ComponentsToRemove = @()

            if ($null -eq $CurrentValues.$CurrentSearchProperty)
            {
                $ComponentsToAdd = $params.$CurrentSearchProperty
            }
            else
            {
                $ComponentsToAdd = @()

                $components = $params.$CurrentSearchProperty | Where-Object -FilterScript {
                    $CurrentValues.$CurrentSearchProperty.Contains($_) -eq $false
                }
                foreach($component in $components)
                {
                    $ComponentsToAdd += $component
                }
                $components = $CurrentValues.$CurrentSearchProperty | Where-Object -FilterScript {
                    $params.$CurrentSearchProperty.Contains($_) -eq $false
                }
                foreach($component in $components)
                {
                    $ComponentsToRemove += $component
                }
            }
            foreach($componentToAdd in $ComponentsToAdd)
            {
                $NewComponentParams = @{
                    SearchTopology = $newTopology
                    SearchServiceInstance = $AllSearchServiceInstances.$componentToAdd
                }
                switch($componentTypes.$CurrentSearchProperty)
                {
                    "IndexComponent" {
                        $NewComponentParams.Add("IndexPartition", $params.Index)
                        if ($params.ContainsKey("RootDirectory") -eq $true)
                        {
                            if ([string]::IsNullOrEmpty($params.RootDirectory) -eq $false)
                            {
                                $NewComponentParams.Add("RootDirectory", $params.RootDirectory)
                            }
                        }
                        New-SPEnterpriseSearchIndexComponent @NewComponentParams
                    }
                }
            }
            foreach($componentToRemove in $ComponentsToRemove)
            {
                $component = Get-SPEnterpriseSearchComponent -SearchTopology $newTopology | `
                    Where-Object -FilterScript {
                        ($_.GetType().Name -eq $componentTypes.$CurrentSearchProperty) `
                            -and ($_.ServerName -eq $componentToRemove) `
                            -and ($_.IndexPartitionOrdinal -eq $params.Index)
                }
                if ($null -ne $component)
                {
                    $component | Remove-SPEnterpriseSearchComponent -SearchTopology $newTopology `
                                                                    -Confirm:$false
                }

            }
        }

        # Apply the new topology
        Set-SPEnterpriseSearchTopology -Identity $newTopology
    }
}

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.UInt32]
        $Index,

        [Parameter(Mandatory = $true)]
        [System.String[]]
        $Servers,

        [Parameter()]
        [System.String]
        $RootDirectory,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ServiceAppName,

        [Parameter()]
        [System.Management.Automation.PSCredential]
        $InstallAccount
    )

    Write-Verbose -Message "Testing Search Index Partition '$Index' settings"

    $CurrentValues = Get-TargetResource @PSBoundParameters

    return Test-SPDscParameterState -CurrentValues $CurrentValues `
                                              -DesiredValues $PSBoundParameters `
                                              -ValuesToCheck @("Servers")
}

Export-ModuleMember -Function *-TargetResource