DSCResources/MSFT_SPSearchManagedProperty/MSFT_SPSearchManagedProperty.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Name,

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

        [Parameter(Mandatory = $true)]
        [ValidateSet("Binary","DateTime","Decimal","Double","Integer","Text","YesNo")]
        [System.String]
        $PropertyType,

        [Parameter()]
        [System.Boolean]
        $Searchable,

        [Parameter()]
        [System.Boolean]
        $Queryable,

        [Parameter()]
        [System.Boolean]
        $Retrievable,

        [Parameter()]
        [System.Boolean]
        $HasMultipleValues,

        [Parameter()]
        [System.Boolean]
        $Refinable,

        [Parameter()]
        [System.Boolean]
        $Sortable,

        [Parameter()]
        [System.Boolean]
        $SafeForAnonymous,

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

        [Parameter()]
        [System.Boolean]
        $TokenNormalization,

        [Parameter()]
        [System.Boolean]
        $NoWordBreaker,

        [Parameter()]
        [System.Boolean]
        $IncludeAllCrawledProperties,

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

        [Parameter()]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure = "Present",

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

    Write-Verbose -Message "Getting Managed Property Setting for '$Name'"

    $result = Invoke-SPDSCCommand -Credential $InstallAccount `
                                  -Arguments @($PSBoundParameters) `
                                  -ScriptBlock {
        $params = $args[0]

        $ssa = Get-SPEnterpriseSearchServiceApplication -Identity $params.ServiceAppName
        if ($null -eq $ssa)
        {
            throw("The specified Search Service Application $($params.ServiceAppName) is `
                   invalid. Please make sure you specify the name of an existing service application."
)
        }
        $managedProperty = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa | `
                                    Where-Object{$_.Name -eq $params.Name}
        if ($null -eq $managedProperty)
        {
            return @{
                Name = $params.Name
                ServiceAppName = $params.ServiceAppName
                PropertyType = $params.PropertyType
                Ensure = "Absent"
            }
        }
        else
        {
            $aliases = $managedProperty.GetAliases()

            $mappedCrawlProperties = $managedProperty.GetMappedCrawledProperties($false)
            $includeAllCrawlProperties = $true
            if ($mappedCrawlProperties)
            {
                $includeAllCrawlProperties = $false
            }
            $results = @{
                Name = $params.Name
                ServiceAppName = $params.ServiceAppName
                PropertyType = $managedProperty.ManagedType
                Searchable = $managedProperty.Searchable
                Queryable = $managedPRoperty.Queryable
                Retrievable = $managedProperty.Retrievable
                HasMultipleValues = $managedProperty.HasMultipleValues
                Refinable = $managedProperty.Refinable
                Sortable = $managedProperty.Sortable
                SafeForAnonymous = $managedProperty.SafeForAnonymous
                Aliases = $aliases
                TokenNormalization = $managedProperty.TokenNormalization
                NoWordBreaker = $managedProperty.NoWordBreaker
                IncludeAllCrawledProperties = $includeAllCrawlProperties
                Ensure = "Present"
            }

            if (!$includeAllCrawlProperties)
            {
                $crawledProperties = @()
                foreach ($mappedProperty in $mappedCrawlProperties)
                {
                    $crawledProperties += $mappedProperty.Name
                }
                $results.Add("CrawledProperties", $crawledProperties)
            }
            return $results
        }
    }
    return $result
}

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

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

        [Parameter(Mandatory = $true)]
        [ValidateSet("Binary","DateTime","Decimal","Double","Integer","Text","YesNo")]
        [System.String]
        $PropertyType,

        [Parameter()]
        [System.Boolean]
        $Searchable,

        [Parameter()]
        [System.Boolean]
        $Queryable,

        [Parameter()]
        [System.Boolean]
        $Retrievable,

        [Parameter()]
        [System.Boolean]
        $HasMultipleValues,

        [Parameter()]
        [System.Boolean]
        $Refinable,

        [Parameter()]
        [System.Boolean]
        $Sortable,

        [Parameter()]
        [System.Boolean]
        $SafeForAnonymous,

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

        [Parameter()]
        [System.Boolean]
        $TokenNormalization,

        [Parameter()]
        [System.Boolean]
        $NoWordBreaker,

        [Parameter()]
        [System.Boolean]
        $IncludeAllCrawledProperties,

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

        [Parameter()]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure = "Present",

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

    Write-Verbose -Message "Setting Managed Property Setting for '$Name'"

    # Obtain information about the current state of the Managed property (if it exists)
    $CurrentValues = Get-TargetResource @PSBoundParameters

    # Validate that the specified crawled properties are all valid and existing
    Invoke-SPDSCCommand -Credential $InstallAccount `
                        -Arguments @($PSBoundParameters, `
                                     $CurrentValues) `
                        -ScriptBlock {
        $params = $args[0]
        $CurrentValues = $args[1]

        #region Pre-Validation
        # Ensure that if we specified that we don't specify any crawled property mapping if we selected to include
        # them all.
        if ($params.IncludeAllCrawledProperties -and $params.CrawledProperties.Length -gt 0)
        {
            throw("You cannot specify values for CrawledProperties if the property `
                IncludeAllCrawledProperties is set to True."
)
        }

        # Ensure that the specified crawled properties exist
        foreach ($mappedCrawlProperty in $params.CrawledProperties)
        {
            $currentCrawlProperty = Get-SPEnterpriseSearchMetadataCrawledProperty -Name $mappedCrawlProperty `
                                                                                  -SearchApplication $params.ServiceAppName
            if (!$currentCrawlProperty)
            {
                throw("The specified crawled property $($mappedCrawlProperty) does not exist. `
                    Please make sure you specify valid existing crawl properties."
)
            }
        }
        #endregion
        $needToRecreate = $false

        # If the property should not be present and is, or if it should be present bu the current property type
        # differs from the desired one.
        if ($params.Ensure -eq "Absent" -and $CurrentValues.Ensure -eq "Present" -or `
        ($params.PropertyType -ne $CurrentValues.PropertyType -and `
        $CurrentValues.Ensure -eq "Present"))
        {
            $managedProperty = Get-SPEnterpriseSearchMetadataManagedProperty -Identity $params.Name `
                                                                         -SearchApplication $params.ServiceAppName

            # In order to delete a Managed PRoperty we need to make sure it doesn't have any crawled properties
            # mapped to it first.
            $managedProperty.DeleteAllMappings()

            # Remove the existing managed property
            Write-Verbose  "Removing Managed Property $($params.Name)"
            Remove-SPEnterpriseSearchMetadataManagedProperty -Identity $params.Name `
                                                             -SearchApplication $params.ServiceAppName `
                                                             -Confirm:$false

            if ($params.PropertyType -ne $CurrentValues.PropertyType)
            {
                Write-Verbose "Detected a change to type from $($currentPropertyType) to `
                               $($params.PropertyType)"

                $needToRecreate = $true
            }
        }

        # Managed Property doesn't exist but should or if we are recreating because of a type change
        if (($CurrentValues.Ensure -eq "Absent" -and $params.Ensure -eq "Present") -or $needToRecreate)
        {
            # Create the new content source and then apply settings to it
            $managedTypeID = [Microsoft.Office.Server.Search.Administration.ManagedDataType]::$($params.PropertyType).value__

            Write-Verbose "Creating a new Managed Property $($params.Name)"
            New-SPEnterpriseSearchMetadataManagedProperty -Name $params.Name `
                                                          -SearchApplication $params.ServiceAppName `
                                                          -Type $managedTypeID
        }

        # Set the specified properties on the Managed Property
        $managedProperty = Get-SPEnterpriseSearchMetadataManagedProperty -Identity $params.Name `
                                                                         -SearchApplication $params.ServiceAppName

        Set-SPEnterpriseSearchMetadataManagedProperty -Identity $managedProperty.Name `
                                                      -SearchApplication $params.ServiceAppName `
                                                      -Retrievable $params.Retrievable `
                                                      -SafeForAnonymous $params.SafeForAnonymous `
                                                      -NoWordBreaker $params.NoWordBreaker

        $managedProperty.HasMultipleValues = $params.HasMultipleValues
        $managedProperty.Searchable = $params.Searchable
        $managedProperty.Queryable = $params.Queryable
        $managedProperty.Refinable = $params.Refinable
        $managedProperty.Sortable = $params.Sortable
        $managedProperty.TokenNormalization = $params.TokenNormalization
        $managedProperty.RespectPriority = !($params.IncludeAllCrawledProperties)
        $managedProperty.OverrideValueOfHasMultipleValues = !($params.IncludeAllCrawledProperties)


        # If alias doesn't already exist, add it
        $currentAliases = $managedProperty.GetAliases()

        foreach($alias in $params.Aliases)
        {
            $currentAlias = $managedProperty.GetAliases() | Where-Object {$_ -eq $alias}
            if(!$currentAlias)
            {
                $managedProperty.AddAlias($alias)
            }
        }
        $managedProperty.Update()

        # Remove the aliases that are different, meaning they currently exist, but were not specified in the config,
        # which means we need to remove them.
        $currentAliases = $managedProperty.GetAliases()

        foreach($alias in $currentAliases)
        {
            if(!$params.Aliases.Contains($alias))
            {
                $managedProperty.DeleteAlias($alias)
            }
        }
        $managedProperty.Update()

        # Generate the Crawled Properties mapping
        $listOfMappedCrawlProperty = [Microsoft.Office.Server.Search.Administration.MappingCollection]::new()
        foreach ($mappedCrawlProperty in $params.CrawledProperties)
        {
            $currentCrawlProperty = Get-SPEnterpriseSearchMetadataCrawledProperty -Name $mappedCrawlProperty `
                                                                                  -SearchApplication $params.ServiceAppName

            $mapping = [Microsoft.Office.Server.Search.Administration.Mapping]::new()
            $mapping.CrawledPropertyName = $mappedCrawlProperty
            $mapping.CrawledPropSet = $currentCrawlProperty.PropSet
            $mapping.ManagedPID = $managedProperty.PId
            $listOfMappedCrawlProperty.Add($mapping)
        }
        $managedProperty.SetMappings($listOfMappedCrawlProperty)
    }
}

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

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

        [Parameter(Mandatory = $true)]
        [ValidateSet("Binary","DateTime","Decimal","Double","Integer","Text","YesNo")]
        [System.String]
        $PropertyType,

        [Parameter()]
        [System.Boolean]
        $Searchable,

        [Parameter()]
        [System.Boolean]
        $Queryable,

        [Parameter()]
        [System.Boolean]
        $Retrievable,

        [Parameter()]
        [System.Boolean]
        $HasMultipleValues,

        [Parameter()]
        [System.Boolean]
        $Refinable,

        [Parameter()]
        [System.Boolean]
        $Sortable,

        [Parameter()]
        [System.Boolean]
        $SafeForAnonymous,

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

        [Parameter()]
        [System.Boolean]
        $TokenNormalization,

        [Parameter()]
        [System.Boolean]
        $NoWordBreaker,

        [Parameter()]
        [System.Boolean]
        $IncludeAllCrawledProperties,

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

        [Parameter()]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure = "Present",

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

    Write-Verbose -Message "Testing Managed Property Setting for '$Name'"

    $PSBoundParameters.Ensure = $Ensure
    $CurrentValues = Get-TargetResource @PSBoundParameters

    return Test-SPDscParameterState -CurrentValues $CurrentValues `
                                    -DesiredValues $PSBoundParameters `
                                    -ValuesToCheck @("Name",
                                                     "PropertyType",
                                                     "Ensure",
                                                     "HasMultipleValues",
                                                     "Retrievable",
                                                     "Searchable",
                                                     "Refinable",
                                                     "Searchable",
                                                     "NoWordBreaker",
                                                     "IncludeAllCrawledProperties",
                                                     "Aliases",
                                                     "Sortable",
                                                     "SafeForAnonymous")
}

Export-ModuleMember -Function *-TargetResource