Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active February 10, 2020 16:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/18c4d9c9f642cf6bcf4e5db2dcae20dc to your computer and use it in GitHub Desktop.
Save joerodgers/18c4d9c9f642cf6bcf4e5db2dcae20dc to your computer and use it in GitHub Desktop.
Rebuilds the SharePoint configuration cache on the specified servers in parallel.
#Requires -Version 4.0
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
$serverNames = Get-SPServer | ? { $_.Role -ne "Invalid" } | % { $_.Address }
workflow Repair-ConfigCache
{
param
(
[parameter(Mandatory=$true)][string[]]$ComputerNames,
[parameter(Mandatory=$true)][string[]]$CachePath
)
foreach -Parallel -ThrottleLimit $($ComputerNames.Count) ( $computerName in $ComputerNames )
{
InlineScript {
$exception = "None"
try
{
# get the original xml file count
$xmlFileCount = (Get-ChildItem -Path $using:CachePath -Filter "*.xml" | Measure-Object).Count
# stop the timer service
Get-Service -Name "SPTimerV4" | Stop-Service -Force
# delete the xml files
Get-ChildItem -Path $using:CachePath -Filter "*.xml" | Remove-Item -Force -Confirm:$False
# update the cache.ini file to 1
Set-Content -Path "$using:CachePath\cache.ini" -Value "1"
# start the timer service
Get-Service -Name "SPTimerV4" | Start-Service
$stopWatch = [System.Diagnostics.StopWatch]::StartNew()
# wait for the .xml files to populate (up to 5 minutes)
while( $stopWatch.ElapsedMilliseconds -lt (1000 * 60 * 5) -and (Get-ChildItem -Path $using:CachePath -Filter "*.xml" | Measure-Object).Count -lt $xmlFileCount )
{
Start-Sleep -Seconds 1
}
}
catch
{
$exception = $_.Exception
}
finally
{
if( (Get-Service -Name "SPTimerV4").Status -ne "Running" )
{
Get-Service -Name "SPTimerV4" | Start-Service
}
[PSCustomObject] @{
"Computer" = $computerName
"StartCacheFileCount" = $xmlFileCount
"FinishCacheFileCount" = (Get-ChildItem -Path $using:CachePath -Filter "*.xml" | Measure-Object).Count
"TimerServiceStatus" = (Get-Service -Name "SPTimerV4").Status
"Exception" = $exception
}
}
} -PSComputerName $computerName
}
}
$configDatabase = Get-SPDatabase -Verbose:$false | ? { $_.TypeName -eq "Configuration Database" }
$cacheDirectory = "C:\ProgramData\Microsoft\SharePoint\Config\$($configDatabase.Id)"
Repair-ConfigCache -ComputerNames $serverNames -CachePath $cacheDirectory | SORT Computer | FT Computer, StartCacheFileCount, FinishCacheFileCount, TimerServiceStatus, Exception -Auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment