19 December 2016

Powershell Tip - How to flush the SharePoint config cache

Every SharePoint administrator knows that flushing the config cache becomes necessary from time to time.  This has traditionally been a very manual process whereby the .xml files are deleted from the cache folder and then the cache.ini file's content is overwritten with a single character.
The following script makes that an automatic process.  The script was generated using my GenPSS utility.

##############################################################################
########################## Clear-SPConfigCache.ps1 ###########################
##############################################################################
Description: === DESCRIPTION HERE ===
Author: Cornelius J. van Dyk | http://blog.cjvandyk.com | @cjvandyk
Version: 1.00
Creation Date: 12/19/2016 12:00:00 AM
Licensed under GPL 3.0 http://opensource.org/licenses/GPL-3.0.

Modified by: 
Version: 
Modified Date: 

Instructions:
  1) Simply run this script to clear the SharePoint config cache.
#############################################################################>

##############################################################################
################################# Parameters #################################
##############################################################################
#[CmdletBinding()]
#Param ([Parameter(Mandatory=$true, HelpMessage="PARAMETER 1 PROMPT HERE?")][ValidateNotNullOrEmpty()][string]$PARM1NAME,
#       [Parameter(Mandatory=$false, HelpMessage="PARAMETER 2 PROMPT HERE?")][ValidateNotNullOrEmpty()][string]$PARM2NAME)

##############################################################################
############################### Thread Handling ##############################
##############################################################################
#Optimize memory optimization through thread re-use.
if ($ver.Version.Major -gt 1)
{
    $Host.Runspace.ThreadOptions = "ReuseThread";
}

##############################################################################
############################### Error Handling ###############################
##############################################################################
#Since the script is intended to run unattended, errors should be suppressed.
$ErrorActionPreference = "SilentlyContinue";

##############################################################################
################################### Modules ##################################
##############################################################################
#Remove the module before re-importing it to ensure the Powershell session 
#gets the latest version.
Remove-Module cjvandyk.quix;
Import-Module cjvandyk.quix;

##############################################################################
################################## Snapins ###################################
##############################################################################
Add-pssnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

##############################################################################
################################# Transcript #################################
##############################################################################
#Ensure a new recording is taken by calling Stop-Recording first.
try
{
    Stop-Recording;
}
catch {}
#Start the transcript without overwriting any existing file, but appends a
#date/time stamp to the log file name.
Start-Recording;

##############################################################################
################################# Variables ##################################
##############################################################################
# === DEFINE VARIABLES HERE ===
$logFile = ".\Clear-SPConfigCache" + "-" + (Get-Date -Format yyyyMMdd-HHmmss) + ".log"
$cacheFolder = "";

##############################################################################
################################## Functions #################################
##############################################################################
# === DEFINE FUNCTIONS HERE ===
#Log output to screen and log file.
function Log($msg)
{
    $t = Get-Date -Format yyyy/MM/dd_HH:mm:ss
    Write-Host $t $msg
    $t + " " + $msg | Out-File -FilePath $logFile -Append 
}

##############################################################################
####################### Ensure script running as Admin #######################
##############################################################################
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") -eq $false)
{
    Write-Host "WARNING! Script is NOT executing in Admin mode.  Terminating script." -ForegroundColor Yellow;
    Stop-Recording;
    Exit(403);
}

##############################################################################
#################################### Main ####################################
##############################################################################
#Stop timer jobs.
Stop-Service SPTimerV4;
$folders = Get-ChildItem C:\ProgramData\Microsoft\SharePoint\Config
#Iterate the config folder to find the cache folder.
foreach ($folder in $folders)
{
    $items = Get-ChildItem $folder.FullName -Recurse;
    foreach ($item in $items)
    {
        if ($item.Name.ToLower() -eq "cache.ini")
        {
            $cacheFolder = $folder.FullName;
            break;  #Folder found now breakout of the loop.
        }
    }
}
#Get all files in the cache folder.
$cacheFolderItems = Get-ChildItem $cacheFolder -Recurse;
#Delete only the .xml files in the cache folder.
foreach ($cacheFolderItem in $cacheFolderItems)
{
    if ($cacheFolderItem -like "*.xml")
    {
        $cacheFolderItem.Delete();
    }
}

#Now delete the content of the cache.ini file.
$cacheFile = Get-Content $cacheFolder\cache.ini;
$cacheFile = 1;
Set-Content $cacheFile -Path $cacheFolder\cache.ini;

#Ensure the operator repeats the process on all SharePoint servers.
Read-Host "Please run this script on all your SharePoint Servers BEFORE pressing ENTER!";
#Restart timer jobs.
Start-Service SPTimerV4;

##############################################################################
#################################### End #####################################
##############################################################################
# Stop recording the transcript.
Stop-Recording;


Enjoy
C

Microsoft Authentication Library (MSAL) Overview

The Microsoft Authentication Library (MSAL) is a powerful library designed to simplify the authentication process for applications that conn...