Clear SharePoint Configuration Cache on one or all SharePoint Servers

Posted: July 1, 2014 in SharePoint PowerShell

Clear the SharePoint Configuration Cache on a SharePoint server or all SharePoint Servers . PowerShell to the rescue!


Clear the SharePoint Configuration Cache on a single Server:

Instructions:

  • Copy code into a text file and save as ClearCacheLocal.ps1 or download rar file and extract.
  • Run as local admin on the server you want to clear the cache.

File:
newdl

Code:


<#====================================================================
 Copyright © 2014, June. Bjørn Roalkvam
 www.SharePointbjorn.com, bjorn80@gmail.com

 The script only targets the server you are running the script on
 and does the following:
 
-Clears the SharePoint Confiugration Cache.
 
 Always test all scripts you find in an test environment prior to production:-),
 you are a responsible SharePoint Administrator afterall!
====================================================================#>


#Start as admin if not started as an admin
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}


#Detect SharePoint version
if((test-path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15") -eq $true)
{
$TimerName = "SharePoint Timer Service"
Write-Host -f DarkCyan "SharePoint 2013 detected"
write-host ""
}

if((test-path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15") -ne $true)
{
$TimerName = "SharePoint 2010 Timer"
Write-Host -f DarkCyan "SharePoint 2010 detected"
write-host ""
}

write-host ""


#Stop timer service
$SharePointimer = Get-Service $TimerName
$SharePointimer.Stop()
while($SharePointimer.Status -eq "Running")
{
write-host "Waiting for timer service to stop.."
sleep 2
$SharePointimer = Get-Service $TimerName
}

#Find and delete XML files in GUID + set cache.ini to 1.
$serverName = hostname
$XMLPath = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config\" #The File path where the deploy package are located.

#Finding the newest Guid folder:
$FindGuidFolder = Get-ChildItem $XMLPath | ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
$ActiveGuidFolder = $FindGuidFolder.name

#Deleting all XML files:
Write-Host -f Cyan "Clearing SharePoint configuration cache by deleting XML files.."
$OldNumberofXML = (Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"}).count
$removeXML = Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"} | % { Remove-Item $_.fullname -Force }
Set-Content (($XMLPath + $ActiveGuidFolder)+ "\" + "cache.ini") "1"



#Start timer service
$SharePointimer = Get-Service $TimerName
$SharePointimer.Start()
while($SharePointimer.Status -eq "Stopped")
{
Write-Host -f cyan "Waiting for timer service to Start.."
sleep 2
$SharePointimer = Get-Service $TimerName
}

$NewNumberofXML = (Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"}).count
while($NewNumberofXML -lt $OldNumberofXML)
{
Write-Host -f cyan "Waiting for new XML files to load into GUID.."
sleep 2
$NewNumberofXML = (Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"}).count
}

write-host ""
Write-Host -f DarkCyan "Completed"

Clear the SharePoint Configuration Cache all SharePoint Servers in the Farm:

Instructions:

  • Copy code into a text file and save as ClearCacheAll.ps1 or download rar file and extract.
  • Run as local admin on any SharePoint server.

File:
newdl

Code:

<#====================================================================
 Copyright © 2014, June. Bjørn Roalkvam
 www.SharePointbjorn.com, bjorn80@gmail.com

 The script only targets all SharePoint servers and does the following:
 
-Clears the SharePoint Confiugration Cache.
 
 Always test all scripts you find in an test environment prior to production:-),
 you are a responsible SharePoint Administrator afterall!
====================================================================#>



#===================================================================================
#Checks wheather the script is running as admin, if not then starts as admin.
#===================================================================================
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

#Add sharepoint pssnapin if it doesnt exist:
if ( (Get-PSSnapin -Name microsoft.sharepoint.powershell -EA "SilentlyContinue") -eq $null )
{
    Add-PsSnapin microsoft.sharepoint.powershell
}





#===================================================================================
#Start
#===================================================================================
Write-Host -f Gray "---------------------------------------------------"
Write-Host -f DarkGreen "Clearing SharePoint cache on all SharePoint Servers"
Write-Host -f Gray "---------------------------------------------------"
Write-Host ""

#Filter SharePoint servers to avoid SQL Server
$AllSPServers = get-spserver | ? { $_.Role -eq "Application"}

#Create individual servername variables
[Array]$ServerArray = "" #Make sure this variable is empty
$ServerArray = $AllSPServers | %{$_.address}


#Create servername variables and give them a value of how many XML files they currently have:
foreach ($serv in $ServerArray)
{
    $XMLPath = "\\" + $serv + "\c$\ProgramData\Microsoft\SharePoint\Config\" #The File path where the deploy package are located.
    $FindGuidFolder = Get-ChildItem $XMLPath | ? { $_.PSIsContainer } | sort lastwritetime -Descending | select -f 1
    $ActiveGuidFolder = $FindGuidFolder.name
    $XMLnumber = (Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"}).count
    [Array]$AllXMLnumbers = [Array]$AllXMLnumbers + $XMLnumber

    New-Variable -Name "OldXML$serv" -Value $XMLnumber   
}


#Saving all servervariables and their XML count in an array varriable
$OldXMLArray = Get-Variable | ? {$_.name -match "oldXML"}
$OldXMLArrayCount = $OldXMLArray.count

#iF THE NUMBER OF XML FILES ARE DIFFERENT ON THE FIRST AND LAST SERVER, THEN EXIT.
if ($OldXMLArray[0].Value -ne $OldXMLArray[-1].Value){Write-Host -F Yellow "Different number of XML files on the first and last server"; read-host; exit}



#===================================================================================
#STOP TIMER SERVICE
#===================================================================================
foreach ($SPServer in $AllSPServers)
{
write-host ""
$ServerName = $SPServer.name
write-host -f Cyan "Stopping timer service on $ServerName.."
Get-Service -Name SPTimerV4 -ComputerName $ServerName | Stop-Service -Force
Write-Host -f Green "OK"
$service = Get-Service -Name SPTimerV4 -ComputerName $servername | Set-Service -Status Stopped
while ($service.Status -eq "Running"){Write-Host "Waiting for service to stop on server $SPServer..";$service = Get-Service -Name SPTimerV4 -ComputerName $ServerName;sleep 3;Write-Host -f Green "OK"}

}


#===================================================================================
#Find and delete XML files in GUID + set cache.ini to 1.
#===================================================================================
[Array]$XMLArray = ""
foreach($server in $ServerArray)
{
$XMLPath = "\\" + $server + "\c$\ProgramData\Microsoft\SharePoint\Config\" #The File path where the deploy package are located.
#Finding the newest Guid folder:
$FindGuidFolder = Get-ChildItem $XMLPath | ? { $_.PSIsContainer } | sort lastwritetime -Descending | select -f 1
$ActiveGuidFolder = $FindGuidFolder.name
write-host ""
#Deleting all XML files:
Write-Host -f Cyan "Deleting XML files on server $Server.."
$removeXML = Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"} | % { Remove-Item $_.fullname -Force }
Write-Host -f Green "OK"
Write-Host ""
Write-Host -f Cyan "Setting config.ini to 1 on server $server.."
Set-Content (($XMLPath + $ActiveGuidFolder)+ "\" + "cache.ini") "1"
Write-Host -f Green "OK"
}


#===================================================================================
#START TIMER SERVICE
#===================================================================================
foreach ($SPServer in $AllSPServers)
{
write-host ""
$ServerName = $SPServer.name
write-host -f Cyan "Starting timer service on $ServerName.."
$starttimer = Get-Service -Name SPTimerV4 -ComputerName $servername | Set-Service -Status Running
$service = Get-Service -Name SPTimerV4 -ComputerName $ServerName
while ($service.Status -eq "Stopped"){Write-Host "Waiting for service to start..";$service = Get-Service -Name SPTimerV4 -ComputerName $ServerName;sleep 3}
Write-Host -f Green "OK"
}


#===================================================================================
#Count files
#===================================================================================
write-host ""
Write-Host -f White "---------------------------------------------------"
foreach($server in $ServerArray)
{
#Count and compare to see if same number of files exist
$XMLPath = "\\" + $server + "\c$\ProgramData\Microsoft\SharePoint\Config\"
$FindGuidFolder = Get-ChildItem $XMLPath | ? { $_.PSIsContainer } | sort lastwritetime -Descending | select -f 1
$ActiveGuidFolder = $FindGuidFolder.name
$NewNumberofXML = (Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"}).count
$count = 0
$OldNumberofXML = $OldXMLArray[$count].value


    #While files are being recreated do:
    while($NewNumberofXML -lt $OldNumberofXML)
    {
    Write-Host -f gray "Recreating XML on $server $NewNumberofXML of $OldNumberofXML completed"
    sleep 2
    $NewNumberofXML = (Get-ChildItem ($XMLPath + $ActiveGuidFolder) | ?{$_.name -like "*xml*"}).count
    }
    #Check if all files are recreated:
    if($NewNumberofXML -eq $OldNumberofXML) {Write-Host -f green "$Server OK : $NewNumberofXML of $OldNumberofXML."}

}

Write-Host -f White "---------------------------------------------------"

Write-Host -f Green "OK - Done - Press the anykey to exit."
Read-Host
exit
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s