Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active May 17, 2023 08:53
Show Gist options
  • Save wpsmith/22e626e92e989c44d0dc to your computer and use it in GitHub Desktop.
Save wpsmith/22e626e92e989c44d0dc to your computer and use it in GitHub Desktop.
PowerShell: Repair SharePoint Distributed Cache Service Instance
<#
.Synopsis
Repairs the Distributed Cache on a Server.
.Description
Repairs the Distributed Cache on a Server by removing the current Distributed Cache Service instance.
.Parameter Server ("ServerName")
The Server running Distributed Cache Service
Type: String
Default: The current server.
.Parameter CachePort ("Port")
The Cache Port.
Type: Int32
Default: 22233.
.Parameter ServiceAccount
The Service Account for the Distributed Cache AppFabric Service.
Type: String
Default: Attempts to retrieve the SP_Farm account from SP Managed Accounts.
.Parameter ExportPath
The path (not filename) to export the Distributed Cache Configuration XML file. The filename will be: <servername>-DistCacheConfig.xml. If set, then the configuration file will export.
Type: String
Default: Current Working Folder.
.Parameter Export
Switch as to whether to export the configuration file or not. If ExportPath is set, this is automatically set to True.
Type: Switch
Default: Current Working Folder.
Default: Current Working Folder.
.Parameter Start
Switch as to whether to export the configuration file or not.
Type: Switch
Default: Current Working Folder.
Default: Current Working Folder.
.Parameter AddFirewallRule ("Firewall")
Switch as to whether to add the recommended firewall rule. If set, it executes the function Add-SPDCFirewallRule which imports NetSecurity module and creates the firewall rule "File and Printer Sharing (echo request - ICMPv4-In)"
Type: Switch
Default: Current Working Folder.
.Example
To repair the current server
Repair-SPDistributedCache -Verbose
.Example
All parameters.
Repair-SPDistributedCache -ServerName MEMWINSHPWEB1 -CachePort 12345 -ServiceAccount "DOMAIN\SP_ServiceAccount" -ExportPath "" -Start -AddFirewallRule {-Verbose}
.Notes
Name: Repair-SPDistributedCache
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Repair-SPDistributedCache
{
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("ServerName")]
[System.String]
$Server,
[Parameter(
Mandatory = $false,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("Port")]
[System.Int32]
$CachePort = 22233,
[Parameter(
Mandatory = $false,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ServiceAccount,
[Parameter(
Mandatory = $false,
Position = 3,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ADDomain,
[Parameter(
Mandatory = $false,
Position = 4,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ExportPath,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Switch]
$Export,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Switch]
$Start,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("Firewall")]
[Switch]
$AddFirewallRule
)
BEGIN {
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
If (!$Server) {
$Server = $env:computername
}
$SPDCInstanceName = "SPDistributedCacheService Name=AppFabricCachingService"
# Start Distributed Cache
#$instanceName = "SPDistributedCacheService Name=AppFabricCachingService"
#$serviceInstance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $instanceName -and ($_.server.name) -eq $env:computername}
#$serviceInstance.Provision()
}
PROCESS {
# Stop/Remove Distributed Cache
Stop-SPDCService -Delete
Write-Host "Getting Servers and Status with SPDistributedCacheService"
$ServerHosts = Get-SPDCServiceInstance | select Server, Status
If ( !$ServerHosts ) {
Write-Host "No servers found, so we need to register the Server as a CacheHost"
Register-SPDCCacheHost -Server $Server -CachePort $CachePort -ServiceAccount $ServiceAccount
} Else {
Write-Verbose $ServerHosts
}
# Maybe Register CacheHost
try {
Write-Verbose "Using Cluster"
Use-CacheCluster
Write-Verbose "Using CacheHostConfig for $($Server):$($CachePort)"
Get-CacheHostConfig -ComputerName $Server -CachePort $CachePort
}
catch
{
Write-Verbose $_.Exception.Message
If ( $_.Exception.Message.Contains("No hosts present in the cluster") ) {
Register-SPDCCacheHost -Server $Server -CachePort $CachePort -ServiceAccount $ServiceAccount
}
}
finally {
Write-Host "Getting Cache Cluster"
Get-CacheHost
}
# Provision Distributed Cache Service
Write-Verbose "Getting the Farm"
$SPFarm = Get-SPFarm
$cacheClusterName = "SPDistributedCacheCluster_" + $SPFarm.Id.ToString()
Write-Verbose "Setting Cache Cluster Name: $cacheClusterName"
Write-Verbose "Creating Cache Cluster Manager"
$cacheClusterManager = [Microsoft.SharePoint.DistributedCaching.Utilities.SPDistributedCacheClusterInfoManager]::Local
If($Verbose) { $cacheClusterManager }
Write-Verbose "Establishing Cache Cluster Info"
$cacheClusterInfo = $cacheClusterManager.GetSPDistributedCacheClusterInfo($cacheClusterName)
If($Verbose) { $cacheClusterManager }
Write-Verbose "Getting Service Instance"
$serviceInstance = Get-SPDCServiceInstance | ? {($_.Server.Name) -eq $Server}
If($Verbose) { $cacheClusterManager }
If(!$cacheClusterInfo -or !$cacheClusterInfo.CacheHostsInfoCollection -or !$ServiceInstance -or $ServiceInstance.Status -eq "Disabled") {
Write-Verbose "Adding $Server to the Distributed Cache Service Instance"
Add-SPDistributedCacheServiceInstance
#$cacheClusterInfo.CacheHostsInfoCollection
$serviceInstance = Get-SPDCServiceInstance | ? {($_.Server.Name) -eq $Server}
If ($serviceInstance.Status = "Online") {
Write-Host "Service Instance ($($serviceInstance.Id)) is Online" -ForegroundColor Green
} Else {
Write-Host "Service Instance could not be brought Online." -ForegroundColor Red
}
}
# Distributed Cache should be ONLINE now
# @todo Monitor Startup to make sure all CacheHosts come up?
<#
Use-CacheCluster
$CacheHost = Get-CacheHost
$NotUpCacheHosts = $CacheHost | ? {$_.Status -ne "UP"} | SELECT HostName, Status
Write-Host "These Cache Hosts are NOT UP"
$NotUpCacheHosts
$timeout = new-timespan -Minutes 5
$sw = [diagnostics.stopwatch]::StartNew()
$HasNotUpCacheHosts = $true
while($sw.elapsed -lt $timeout -and $NotUpCacheHosts) {
$CacheHost = Get-CacheHost
$NotUpCacheHosts = $CacheHost | ? {$_.Status -ne "UP"} | SELECT HostName, Status
Write-Host "." -NoNewline
}
#$NotUpCacheHosts = $CacheHost | ? {$_.Status -ne "UP"} | SELECT HostName, Status
#$UpCacheHosts = $CacheHost | ? {$_.Status -eq "UP"} | SELECT HostName, Status
#>
# Maybe Export Configuration
If ($Export -or $ExportPath) {
If ($ExportPath) {
$ConfigPath = $ExportPath
} Else {
$ConfigPath = Convert-Path .
}
Write-Host "Exporting Cluster Configuration"
Export-CacheClusterConfig "$($ConfigPath)\$($Server)-DistCacheConfig.xml"
}
<#
# Verify access via Get-CacheAllowedClientAccounts
# @todo Test whether this is possible using AD Module, easier
# If Import-Module ActiveDirectory works,
# then use AD commandlets,
#Else use ADSI LDAP to try to check
#>
If ($ServiceAccount) {
Write-Host "Testing whether service account has access"
# Check if Current User has DOMAIN ADMIN rights
If(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Domain Admins")) {
# Let's use the Active Directory Module, Maybe
$ADModule = Get-Module -ListAvailable | ? {$_.Name -eq "ActiveDirectory"}
If ($ADModule) {
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
# Have access to AD
# Determine whether Member, Output result
If ( Get-CacheAllowedClientAccounts | ForEach-Object `
{ Is-SPDCUserGroupMemberADSI -Group $_ -User $ServiceAccount } ) {
Write-Host "Service Account ($($ServiceAccount)) HAS access to $($Group)!" -ForegroundColor Green
} Else {
Write-Host "Service Account ($($ServiceAccount)) DOES NOT have access to $($Group)!" -ForegroundColor Red
}
} Else {
Write-Host "Unable to determine whether the service account has access because the Active Directory Module cannot be imported." -ForegroundColor Red
Get-SPDCGroupMembershipDirections
}
} Else {
Write-Host "Unable to determine whether the service account has access because you do not have DOMAIN ADMIN rights." -ForegroundColor Red
Get-SPDCGroupMembershipDirections
}
}
# Maybe Start Cluster
If ($Start) {
Write-Host "Starting Cache Cluster"
#Start-CacheCluster
Start-CacheHost -HostName $Server -CachePort $CachePort
}
# Maybe Add Firewall Rule
If ($AddFirewallRule) {
Add-SPDCFirewallRule
}
}
}
<#
.Synopsis
Interactive/Manual process to determine whether a User/Service Account is part of a Group.
.Description
Interactive/Manual process to determine whether a User/Service Account is part of a Group via ADSI.
.Example
No parameters required.
Get-SPDCGroupMembershipDirections
.Notes
Name: Get-SPDCGroupMembershipDirections
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Get-SPDCGroupMembershipDirections
{
Write-Host "Let's see if we can manually determine if the Service Account has rights..."
$Group = Read-Host "Enter the Group AD Distinguished Name (e.g., CN=GroupName,OU=Groups,DC=domain,DC=com)"
$ServiceAccount = Read-Host "Enter the Service Account AD Distinguished Name (e.g., CN=UserName,OU=Users,DC=domain,DC=com)"
If ($Group.IsMember($user.ADsPath) -eq $True){
Write-Host "$($ServiceAccount) is member of $($Group)" -ForegroundColor Green
}
else{
Write-Host "$($ServiceAccount) is not member of $($Group)" -ForegroundColor Red
}
}
<#
.Synopsis
Sets the service account for SharePoint Distributed Cache.
.Description
Sets the service account for SharePoint Distributed Cache.
.Parameter ServiceAccount
The Service Account for the Distributed Cache AppFabric Service.
Type: String
Mandatory
Default: Attempts to retrieve the SP_Farm account from SP Managed Accounts.
.Example
All parameters.
Set-SPDCServiceAccount -ServiceAccount "DOMAIN\SP_ServiceAccount"
.Notes
Name: Set-SPDCServiceAccount
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Set-SPDCServiceAccount
{
[CmdletBinding()]
param(
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ServiceAccount #domain_name\user_name
)
PROCESS {
$farm = Get-SPFarm
$cacheService = $farm.Services | where {$_.Name -eq "AppFabricCachingService"}
$accnt = Get-SPManagedAccount -Identity $ServiceAccount
$cacheService.ProcessIdentity.CurrentIdentityType = "SpecificUser"
$cacheService.ProcessIdentity.ManagedAccount = $accnt
$cacheService.ProcessIdentity.Update()
$cacheService.ProcessIdentity.Deploy()
}
}
<#
.Synopsis
Gets and returns the SharePoint Distributed Cache Instance.
.Description
Gets and returns the SharePoint Distributed Cache Instance.
.Example
No parameters required.
Get-SPDCServiceInstance
.Notes
Name: Get-SPDCServiceInstance
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Get-SPDCServiceInstance
{
[CmdletBinding()]
param()
$SPDCInstanceName = "SPDistributedCacheService Name=AppFabricCachingService"
$Instance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $SPDCInstanceName}
Return $Instance
}
<#
.Synopsis
Adds the recommended firewall rule for Distributed Cache when using more than one (1) server.
.Description
Adds the recommended firewall rule for Distributed Cache when using more than one (1) server. If set, it executes the function Add-SPDCFirewallRule which imports NetSecurity module and creates the firewall rule "File and Printer Sharing (echo request - ICMPv4-In)"
.Example
No parameters required.
Add-SPDCFirewallRule
.Notes
Name: Add-SPDCFirewallRule
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Add-SPDCFirewallRule
{
[CmdletBinding()]
param()
Import-Module NetSecurity
Set-NetFirewallRule -DisplayName "File and Printer Sharing (echo request - ICMPv4-In)" -Enabled True
}
<#
.Synopsis
Registers a server as a Cache Host.
.Description
Registers a server as a Cache Host.
.Parameter Server ("HostName")
The name of the cache host (or server) to register in the cache cluster.
Type: String
Mandatory
.Parameter CachePort ("Port")
The Cache Port.
Type: Int32
Default: 22233.
.Parameter ConnectionString
The connection string to the provider that contains the cache cluster configuration store.
Type: String
Default: Attempts to get the Connection String from the registry: "HKLM:\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration\ConnectionString".
.Parameter ServiceAccount
The Service Account for the Distributed Cache AppFabric Service.
Type: String
Mandatory
Default: Attempts to retrieve the SP_Farm account from SP Managed Accounts.
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_Farm account.
Register-SPDCCacheHost -Server MEMWINSHPWEB1 -Port 12345
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_NetAppPool account.
Register-SPDCCacheHost -Server MEMWINSHPWEB1 -Port 12345 -ServiceAccount SP_NetAppPool
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_NetAppPool account with the connection string.
Register-SPDCCacheHost -Server MEMWINSHPWEB1 -Port 12345 -ServiceAccount SP_NetAppPool -ConnectionString
.Notes
Name: Register-SPDCCacheHost
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Register-SPDCCacheHost
{
[CmdletBinding()]
param(
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("HostName")]
[System.String]
$Server,
[Parameter(
Mandatory = $false,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("Port")]
[System.Int32]
$CachePort = 22233,
[Parameter(
Mandatory = $false,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ConnectionString,
[Parameter(
Mandatory = $false,
Position = 3,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ServiceAccount
)
BEGIN {
If (!$ConnectionString) {
$ConnectionString = Get-SPDCAFConnectionString
}
If ($ConnectionString) {
Write-Verbose "Retrieved ConnectionString from registry: $ConnectionString"
} Else {
Write-Host "Unable to retrieve a valid Connection String from the registry." -ForegroundColor Red
Return
}
If (!$ServiceAccount) {
$ServiceAccount = Get-SPDCFarmAccount
}
}
PROCESS {
try {
Write-Verbose "Registering Host $Server with ConnectionString: $ConnectionString"
Register-CacheHost `
-ProviderType "SPDistributedCacheClusterProvider" `
-ConnectionString "$ConnectionString" `
-Account $ServiceAccount `
-CachePort $CachePort `
-ClusterPort 22234 `
-ArbitrationPort 22235 `
-ReplicationPort 22236 `
-HostName $Server
}
catch {
Write-Verbose $_.Exception.Message
If ( $_.Exception.Message.Contains("already present in cluster configuration") ) {
# Do Nothing!
}
}
}
}
<#
.Synopsis
Unregisters a server as a Cache Host.
.Description
Unregisters a server as a Cache Host.
.Parameter Server ("HostName")
The name of the cache host (or server) to register in the cache cluster.
Type: String
Mandatory
.Parameter CachePort ("Port")
The Cache Port.
Type: Int32
Default: 22233.
.Parameter ConnectionString
The connection string to the provider that contains the cache cluster configuration store.
Type: String
Default: Attempts to get the Connection String from the registry ( "HKLM:\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration\ConnectionString") using Get-SPDCAFConnectionString.
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_Farm account.
UnRegister-SPDCCacheHost -Server MEMWINSHPWEB1 -Port 12345
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_NetAppPool account.
UnRegister-SPDCCacheHost -Server MEMWINSHPWEB1 -Port 12345 -ServiceAccount SP_NetAppPool
.Notes
Name: UnRegister-SPDCCacheHost
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function UnRegister-SPDCCacheHost
{
[CmdletBinding()]
param(
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("ServerName")]
[System.String]
$Server,
[Parameter(
Mandatory = $false,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("Port")]
[System.Int32]
$CachePort = 22233,
[Parameter(
Mandatory = $false,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ConnectionString
)
BEGIN {
If (!$ConnectionString) {
$ConnectionString = Get-SPDCAFConnectionString
}
If ($ConnectionString) {
Write-Verbose "Retrieved ConnectionString from registry: $ConnectionString"
} Else {
Write-Host "Unable to retrieve a valid Connection String from the registry." -ForegroundColor Red
Return
}
}
PROCESS {
Unregister-CacheHost -ProviderType "SPDistributedCacheClusterProvider" -ConnectionString $ConnectionString -HostName $Server
}
}
<#
.Synopsis
Attempts to get the Connection String from the registry.
.Description
Attempts to get the Connection String from the registry ( "HKLM:\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration\ConnectionString").
.Example
No parameters required.
Get-SPDCAFConnectionString
.Notes
Name: Get-SPDCAFConnectionString
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Get-SPDCAFConnectionString
{
[CmdletBinding()]
param()
Write-Verbose "Registering Host $Server with ConnectionString: $ConnectionString"
Write-Verbose $ConnectionString
Return (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration" -Name ConnectionString).ConnectionString
}
<#
.Synopsis
Determines whether a user is a member of a group.
.Description
Determines whether a user is a member of a group using ADSI LDAP.
.Parameter $ADDomain
The Active Directory domain.
Type: String
Mandatory
.Parameter User
The AD string for a user.
Type: String
Mandatory
.Parameter Group
The group being checked for membership.
Type: String
Default: WSS_Admin_WPG.
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_Farm account.
IsMember -ADDomain "domain2.fabrikam.com" -User "CN=Users,DC=domain2,DC=fabrikam,DC=com" -Group "WSS_WPG"
.Notes
Name: Add-SPCacheHost
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Is-SPDCUserGroupMemberADSI
{
Param (
[Parameter(
Mandatory = $true,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("UserName")]
[System.String]
$User, # "username"
[Parameter(
Mandatory = $false,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$Group = "WSS_Admin_WPG"
)
BEGIN {
If ($User.Contains("\")) {
$ss = $User.split('\')
$UserName = $ss[1]
$DomainName = $ss[0]
}
}
PROCESS {
If (
Get-ADUser `
-Filter "memberOf -RecursiveMatch '$((Get-ADGroup $Group).DistinguishedName)'" `
-SearchBase $((Get-ADUser $UserName).DistinguishedName)
) {
Return $true
}
#Group Not found, send False
Return $false
}
}
<#
.Synopsis
Determines whether a user is a member of a group.
.Description
Determines whether a user is a member of a group using ADSI LDAP.
.Parameter $ADDomain
The Active Directory domain.
Type: String
Mandatory
.Parameter User
The AD string for a user.
Type: String
Mandatory
.Parameter Group
The group being checked for membership.
Type: String
Default: WSS_Admin_WPG.
.Example
Register MEMWINSHPWEB1:12345 as a Cache Host using the SP_Farm account.
Is-SPDCUserGroupMember -ADDomain "domain2.fabrikam.com" -User "CN=Users,DC=domain2,DC=fabrikam,DC=com" -Group "WSS_WPG"
.Notes
Name: Add-SPCacheHost
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Is-SPDCUserGroupMemberADSI
{
Param (
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$ADDomain,
[Parameter(
Mandatory = $true,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$User,
[Parameter(
Mandatory = $false,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[System.String]
$Group = "WSS_Admin_WPG"
)
PROCESS {
$User = [ADSI]"LDAP://$($ADDomain)/$($User)"
$Groups = $User.MemberOf | ForEach-Object {[ADSI]"LDAP://$_"}
If ($Groups) {
Return $true
}
#Group Not found, send False
Return $false
}
}
<#
.Synopsis
Starts the Distributed Cache Service Instance.
.Description
Starts the Distributed Cache Service Instance.
.Parameter Server ("ServerName")
The Server running Distributed Cache Service
Type: String
Default: The current server.
.Example
No parameters required.
Start-SPDCService
.Example
Designating the actual server.
Start-SPDCService MEMWINSHPWEB1
Start-SPDCService -Server MEMWINSHPWEB1
.Notes
Name: Start-SPDCService
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Start-SPDCService
{
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("ServerName")]
[System.String]
$Server
)
BEGIN {
If (!$Server) {
$Server = $env:computername
}
}
PROCESS {
$instanceName ="SPDistributedCacheService Name=AppFabricCachingService"
$serviceInstance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $instanceName -and ($_.server.name) -eq $Server}
If ($serviceInstance) {
$serviceInstance.Provision()
}
}
}
<#
.Synopsis
Stops the Distributed Cache Service Instance.
.Description
Stops the Distributed Cache Service Instance.
.Parameter Server ("ServerName")
The Server running Distributed Cache Service
Type: String
Default: The current server.
.Example
No parameters required.
Stop-SPDCService
.Example
Designating the actual server.
Stop-SPDCService MEMWINSHPWEB1
Stop-SPDCService -Server MEMWINSHPWEB1
.Notes
Name: Stop-SPDCService
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Stop-SPDCService
{
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("ServerName")]
[System.String]
$Server,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Switch]
$Delete
)
BEGIN {
If (!$Server) {
$Server = $env:computername
}
}
PROCESS {
$instanceName ="SPDistributedCacheService Name=AppFabricCachingService"
$serviceInstance = Get-SPServiceInstance | ? {($_.service.tostring()) -eq $instanceName -and ($_.server.name) -eq $Server}
If ($serviceInstance) {
#Stop-SPDCServiceInstance -Graceful
$serviceInstance.Unprovision()
If ($Delete) {
#Remove-SPDistributedCacheServiceInstance
$serviceInstance.Delete()
}
}
}
}
<#
.Synopsis
Attempts to get the Farm Account name from the SP Managed Accounts list.
.Description
Attempts to get the Farm Account name from the SP Managed Accounts list by looking for "farm" within the SP Managed Account names.
.Example
No parameters required.
Get-SPDCAFConnectionString
.Notes
Name: Get-SPDCAFConnectionString
Author: Travis Smith
LastEdit: 06/04/2014
#>
Function Get-SPDCFarmAccount
{
[CmdletBinding()]
param()
$FarmAccount = (Get-SPManagedAccount | where {$_.Username.ToLower().Contains("farm")}).UserName
If ($FarmAccount) {
Return $FarmAccount
}
Return ""
}
@potatoqualitee
Copy link

nice work! this would make a wonderful module, like SPConfigFixer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment