$SPFarm = Get-SPFarm

$cacheClusterName = “SPDistributedCacheCluster_” + $SPFarm.Id.ToString()

$cacheClusterManager = [Microsoft.SharePoint.DistributedCaching.Utilities.SPDistributedCacheClusterInfoManager]::Local

$cacheClusterInfo = $cacheClusterManager.GetSPDistributedCacheClusterInfo($cacheClusterName);

$instanceName =”SPDistributedCacheService Name=AppFabricCachingService”

$serviceInstance = Get-SPServiceInstance | ? {($_.Service.Tostring()) -eq $instanceName -and ($_.Server.Name) -eq $env:computername}

if([System.String]::IsNullOrEmpty($cacheClusterInfo.CacheHostsInfoCollection))

{

#here’s the key. we can’t provision, unprovision, start, or stop a Cache Service because we still have a Cache Service that have no server attached

$serviceInstance.Delete()

Add-SPDistributedCacheServiceInstance

$cacheClusterInfo.CacheHostsInfoCollection

}