SharePointDistributedCache.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<#
.Synopsis
   Distributed Cache Utilities
.DESCRIPTION
   Collection of Advanced Functions for working with Distributed Cache
   Depends on PS Remoting available on every server in the farm
.NOTES
 File Name : SharePointDistributedCache.psm1
 Author : Spencer Harbar (spence@harbar.net)
 Requires : PowerShell Version 4.0
#>




Function Update-DistributedCacheIdentity {
<#
.Synopsis
   Updates the Distributed Cache Service Account Identity.
.DESCRIPTION
   Updates the Distributed Cache Service Account Identity.
   Can be run from any Server in the Farm.
   If the Farm Service exists and a Cache Host exists, it will deploy after ensuring there is only
   one Cache Host and then re-add the other Cache Hosts
   If the Farm Service exists but zero Cache Hosts exist, and we include -InitialIdentity it will update
   the identity but not deploy, which supports a SharePoint 2016 MinRole Farm build pattern. Should never be
   used afer creating the Cache Cluster
 
   If Deploy fails we put the Cluster back as it was
.EXAMPLE
   Update-DistributedCacheIdentity -ManagedAccountName "DOMAIN\user" -Credential $Credential
.EXAMPLE
   Update-DistributedCacheIdentity -ManagedAccountName "DOMAIN\user" -Credential $Credential -InitialIdentity
#>


    [CmdletBinding()]
    Param (  
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Name of an existing Managed Account to use as the service identity, e.g. DOMAIN\user",
                   Position=0)]
        [ValidateNotNullorEmpty()]
        [String]
        $ManagedAccountName,
        
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Credential of a Shell Admin",
                   Position=1)]
        [ValidateNotNullorEmpty()]
        [PSCredential]
        $Credential,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="For SharePoint 2016 Farms where no Distributed Cache machine has yet been added",
                   Position=2)]
        [Switch]$InitialIdentity
    ) 

    Begin {
        Write-Output "$(Get-Date -Format T) : Initiated Distributed Cache Service Identity change." 
        if ((Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
            Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell"
        }     
        $ScriptBlock = {

            Write-Output "$(Get-Date -Format T) : Initiated Distributed Cache Service Identity deploy." 
            Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell"

            try {
                $Farm = Get-SPFarm
                $DistributedCacheService = $Farm.Services | Where-Object { $_.TypeName -eq "Distributed Cache" }
                Write-Output "$(Get-Date -Format T) : This will take approximately 8 minutes..."
                $DistributedCacheService.ProcessIdentity.Deploy()
                Write-Output "$(Get-Date -Format T) : Distributed Cache Service Account Change Deployed!"

            } catch {
                Write-Error "ERROR: We failed during changing the Distributed Cache Service Account."
                $_.Exception.Message
            }
        }
    }

    Process {
        # Grab the Farm, Managed Account, Farm Service and the Servers running DC
        $Farm = Get-SPFarm
        $ManagedAccount = Get-SPManagedAccount -Identity $ManagedAccountName
        $DistributedCacheService = $Farm.Services | Where-Object { $_.TypeName -eq "Distributed Cache" }
        $DistributedCacheServers = Get-SPServer | 
                                   Where-Object {($_.ServiceInstances | ForEach TypeName) -eq "Distributed Cache"} | 
                                   ForEach Address

        # if the service exsits the farm, we can change the identity...
        # but only if no instance has ever been started, otherwise this cause "unkown" after host is brought up
        If ($DistributedCacheService) {

            # No servers running DC, we fall thru as there is no further work to do
            # unless InitialIdentity is specified, in which case we update but don't deploy
            If ($DistributedCacheServers.Count -eq 0) {
                If ($InitialIdentity) {
                    Write-Output "$(Get-Date -Format T) : Configuring Distributed Cache Service Account..." 
                    $DistributedCacheService.ProcessIdentity.CurrentIdentityType = "SpecificUser"
                    $DistributedCacheService.ProcessIdentity.ManagedAccount = $ManagedAccount
                    $DistributedCacheService.ProcessIdentity.Update() 
                    Write-Output "$(Get-Date -Format T) : Distributed Cache Service Account Updated!"    
                }
                Else {
                    Write-Output "$(Get-Date -Format T) : No servers in this farm are running Distributed Cache!"
                }
            }
            # there is at least one server
            Else {
                Write-Output "$(Get-Date -Format T) : Configuring Distributed Cache Service Account..." 
                $DistributedCacheService.ProcessIdentity.CurrentIdentityType = "SpecificUser"
                $DistributedCacheService.ProcessIdentity.ManagedAccount = $ManagedAccount
                $DistributedCacheService.ProcessIdentity.Update() 
                Write-Output "$(Get-Date -Format T) : Distributed Cache Service Account Updated!"                 
                # If More than one DC server, remove all but one
                For ($i=0; $i -le ($DistributedCacheServers.Count - 2); $i++) {
                    Remove-DistributedCache -ComputerName $DistributedCacheServers[$i] -Credential $Credential
                }
                        
                # Get the remaining server
                $ChangeIdentityServer = Get-SPServer | 
                                        Where-Object {($_.ServiceInstances | ForEach TypeName) -eq "Distributed Cache"} | 
                                        ForEach Address

                #run the deploy() script block
                Invoke-Command -ComputerName $ChangeIdentityServer -Credential $Credential -Authentication Credssp `
                               -ScriptBlock $ScriptBlock

                # add back the other DC servers
                ForEach ($DistributedCacheServer in $DistributedCacheServers) {
                    If ($DistributedCacheServer -ne $ChangeIdentityServer) {
                        Add-DistributedCache -ComputerName $DistributedCacheServer -Credential $Credential
                    }
                }
            } 
        }
        Else {
            Write-Output "$(Get-Date -Format T) : Distributed Cache Service does not yet exist in the Farm!"
        }
    }

    End {}
}

Function Update-DistributedCacheSize {
<#
.Synopsis
   Updates the Cache Size in MB on every Cache Host in the Cluster.
.DESCRIPTION
   Updates the Cache Size in MB on every Cache Host in the Cluster.
   Can be run from any server in the farm.
   Stops the Service Instance where ever it is running, configures the Cache Size and Starts the Service Instances
 
   SP2016 Note:
   Once we stop the SIs each machine will be out of compliance with minrole.
   All will be good once it's complete, however we could use Stop-SPService first which sets AutoProvision to false.
   Then Start-SPService after changing the cache size. that way there can never be any "conflict"
   But that only works with SP16 and MinRole.
   This way it works no matter which version or if we are MinRole or Custom
   The time it takes is so small, and you shoyuld be aware of when timer jobs are running anyway!
 
.EXAMPLE
   Update-DistributedCacheSize -CacheSizeInMB 500 -Credential $Credential
#>


    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Cache Size in MB to configure on each Cache Host",
                   Position=0)]
        [ValidateNotNullorEmpty()]
        [Int]
        $CacheSizeInMB,

        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Credential of a Shell Admin",
                   Position=1)]
        [ValidateNotNullorEmpty()]
        [PSCredential]
        $Credential
    )

    Begin {
        Write-Output "$(Get-Date -Format T) : Initiated Distributed Cache Service Cache Size change."
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
         $ScriptBlock = {
            param (
                [Int]$CacheSizeInMB
            )
            Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell"
            Update-SPDistributedCacheSize -CacheSizeInMB $CacheSizeInMB
         }
    }

    Process {
        try {
            # Get the servers running DC
            $DistributedCacheServers = @( Get-SPServer | `
                                       Where-Object {($_.ServiceInstances | ForEach TypeName) -eq "Distributed Cache"} | `
                                       ForEach Address )

            # Stop service instances
            Stop-DistributedCacheCluster

            # Update the Cache Size
            Write-Output "$(Get-Date -Format T) : Changing Distributed Cache Service Cache Size..."
            Invoke-Command -ComputerName $DistributedCacheServers[0] -Credential $Credential -Authentication Credssp `
                           -ArgumentList $CacheSizeInMB -ScriptBlock $ScriptBlock

            # Start service instances
            Start-DistributedCacheCluster
        }
        catch {
            Write-Error "ERROR: We failed during changing the Distributed Cache Size."
            $_.Exception.Message
        }
    }

    End {
        Write-Output "$(Get-Date -Format T) : Completed Distributed Cache Service Cache Size Change!"
    }
}

Function Start-DistributedCacheCluster {
<#
.Synopsis
   Starts the Distributed Cache Service Instance on every server it exists
.DESCRIPTION
   Starts the Distributed Cache Service Instance on every server it exists.
   Can be run from any server in the farm.
   Used in the 'update' pattern, for example changing the Cache Size.
   It is expected that a previous Cache Cluster has been created using
   Add-SPDistributedCacheServiceInstance or the Server Role Distributed Cache
.EXAMPLE
   Start-DistributedCacheCluster
#>


    [CmdletBinding()]
    Param ()

    Begin {
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
    }

    Process {
        Write-Output "$(Get-Date -Format T) : Starting Distributed Cache Service Instance on all servers..."
        Get-SPServiceInstance | Where-Object { $_.TypeName -eq "Distributed Cache" } | Start-SPServiceInstance | Out-Null
        While (Get-SPServiceInstance | Where-Object { $_.TypeName -eq "Distributed Cache" -and $_.Status -ne "Online" }) {
            Start-Sleep -Seconds 15
        }
    }

    End {
        Write-Output "$(Get-Date -Format T) : All Distributed Cache Service Instances started!"
    }
}

Function Stop-DistributedCacheCluster {
<#
.Synopsis
   Stops the Distributed Cache Service Instance on every server it exists
.DESCRIPTION
   Stops the Distributed Cache Service Instance on every server it exists.
   Can be run from any server in the farm.
   Used in the 'update' pattern, for example changing the Cache Size.
   It is expected that a previous Cache Cluster has been created using
   Add-SPDistributedCacheServiceInstance or the Server Role Distributed Cache
.EXAMPLE
   Stop-DistributedCacheCluster
#>


    [CmdletBinding()]
    Param ()

    Begin {
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
    }

    Process {
        Write-Output "$(Get-Date -Format T) : Stopping Distributed Cache Service Instance on all servers..."
        Get-SPServiceInstance | Where-Object { $_.TypeName -eq "Distributed Cache" } | Stop-SPServiceInstance -Confirm:$false | Out-Null
        While (Get-SPServiceInstance | Where-Object { $_.TypeName -eq "Distributed Cache" -and $_.Status -ne "Disabled" }) {
            Start-Sleep -Seconds 15
        }
    }

    End {
        Write-Output "$(Get-Date -Format T) : All Distributed Cache Service Instances stopped!"
    }
}

Function Get-DistributedCacheStatus {
<#
.Synopsis
   Displays the status of the Cache Cluster and optionally each Cache Host
.DESCRIPTION
   Displays the status of the Cache Cluster and optionally each Cache Host.
   Can be run from any server in the farm.
   Status of each host is the key - Up is All Good. Anything else means a broken cache cluster.
   Pass in -CacheHostConfiguration to view additional settings for each Cache Host (e.g. Cache Size).
 
   # Note (the bool should be a switch - won't flow thru as param)
 
.EXAMPLE
   Get-DistributedCacheStatus -Credential $Credential
.EXAMPLE
   Get-DistributedCacheStatus -Credential $Credential -CacheHostConfiguration
#>


    [CmdletBinding()]
    Param (  
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Credential of a Shell Admin",
                   Position=0)]
        [ValidateNotNullorEmpty()]
        [PSCredential]
        $Credential,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Output individual Cache Host Configuration",
                   Position=1)]
        [Switch]
        $CacheHostConfiguration
    ) 

    Begin {
        if ((Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
            Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell"
        }
        $ScriptBlock = {

            param (
                [String[]]$DistributedCacheServers, 
                [Bool]$CacheHostConfiguration = $false
            )
            
            Use-CacheCluster
            #status of each host, first hit always seems to error on one of them!
            $DistributedCacheServers | ForEach {Get-AFCacheHostStatus -ComputerName $_ -CachePort 22233} | Format-Table

            if ($CacheHostConfiguration) {
                #config of each host
                $DistributedCacheServers | ForEach {Get-AFCacheHostConfiguration -ComputerName $_ -CachePort 22233} | 
                                           Select HostName, Size, HighWatermark, LowWatermark, IsLeadHost | Format-Table
            }

        }
        
    }

    Process {

        $DistributedCacheServers = @(Get-SPServer | 
                                   Where-Object {($_.ServiceInstances | ForEach TypeName) -eq "Distributed Cache"} | 
                                   ForEach Address)

        Invoke-Command -ComputerName $DistributedCacheServers[0] -Credential $Credential -Authentication Credssp `
                       -ArgumentList $DistributedCacheServers, $CacheHostConfiguration -ScriptBlock $ScriptBlock
    }

    End {}
}

Function Remove-DistributedCache {
<#
.Synopsis
   Removes a Cache Host from the Cluster and the Service Instance.
.DESCRIPTION
   Removes a Cache Host from the Cluster and the Service Instance.
   Can be run from any server in the farm.
   Waits for Port to free up
   Used in the 'identity' pattern which requires only a single Cache Host.
   Also useful when working more generally with Distributed Cache
.EXAMPLE
   Remove-DistributedCache -ComputerName "FABSP01" -Credential $Credential
#>


    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Computer Name of a machine to remove as a Cache Host/Service Instance",
                   Position=0)]
        [ValidateNotNullorEmpty()]
        [String]
        $ComputerName,

        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Credential of a Shell Admin",
                   Position=1)]
        [ValidateNotNullorEmpty()]
        [PSCredential]
        $Credential
    ) 

    Begin {
        $ScriptBlock = {
            $server = $env:ComputerName
            Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell"
            
            try {
                Write-Output "$(Get-Date -Format T) : Removing server $server as Distributed Cache host..."
                Remove-SPDistributedCacheServiceInstance -ErrorAction Stop
                $Count = 0
                $MaxCount = 5
                While ( ($Count -lt $MaxCount) -and (Get-NetTCPConnection -LocalPort 22234 -ErrorAction SilentlyContinue).Count -gt 0 ) {
                    Write-Output "$(Get-Date -Format T) : Waiting on port to free up..."
                    Start-Sleep -Seconds 30
                    $Count++
                }
                Write-Output "$(Get-Date -Format T) : Removed server $server as Distributed Cache host!"

            } catch {
                Write-Error "ERROR: We failed during removing cache host cluster on $server."
                $_.Exception.Message
            }
        }
    }

    Process {
        Invoke-Command -ComputerName $ComputerName -Credential $Credential -Authentication Credssp -ScriptBlock $ScriptBlock
    }

    End {}
}

Function Add-DistributedCache {
<#
.Synopsis
   Adds a Cache Host to the Cluster and the Service Instance.
.DESCRIPTION
   Adds a Cache Host to the Cluster and the Service Instance.
   Can be run from any server in the farm.
   Used in the 'identity' pattern which requires only a single Cache Host.
   Also useful when working more generally with Distributed Cache
.EXAMPLE
   Add-DistributedCache -ComputerName "FABSP01" -Credential $Credential
#>


    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Computer Name of a machine to add as a Cache Host/Service Instance",
                   Position=0)]
        [ValidateNotNullorEmpty()]
        [String]
        $ComputerName,

        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="The Credential of a Shell Admin",
                   Position=1)]
        [ValidateNotNullorEmpty()]
        [PSCredential]
        $Credential
    ) 

    Begin {
        $ScriptBlock = {
            $server = $env:ComputerName
            Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell"
            
            try {
                Write-Output "$(Get-Date -Format T) : Adding server $server as Distributed Cache host..."
                Add-SPDistributedCacheServiceInstance -ErrorAction Stop
                Write-Output "$(Get-Date -Format T) : Added server $server as Distributed Cache host!"
            } catch {
                Write-Error "ERROR: We failed during adding cache host cluster on $server."
                $_.Exception.Message
            }
        }
    }

    Process {
        Invoke-Command -ComputerName $ComputerName -Credential $Credential -Authentication Credssp -ScriptBlock $ScriptBlock
    }

    End {}
}

#EOF