Posted on:
Categories: PowerShell;SharePoint


Microsoft has recently changed their guidance for AppFabric in regards to patching. Initially they were planning on patching AppFarbic using the standard SharePoint Cumulative Updates. They have recently changed this guidance to allow us to patch AppFabric separately. Noted Here

At the time of this blog the latest AppFabric Cumulative update is CU5


We have created a script which will update AppFabric on a specific server.

The following script will:

  • Stop the Distributed Cache Service on the current server.
  • Look for a Cumulative Update executable in the same directory as the script and execute it. The script will look for a executable with a name that matches AppFabric1.1*.exe.
  • Update the AppFabric config to enable Garbage Collection.
  • Start the Distributed Cache Service on the current server.
 # Name:            Update-AppFabric.ps1 
 # Description:      This script will patch and enable Garbage Collection in AppFabric
 # Usage:         Run script, make sure that the AppFabric CU is in the same directory
 #   as the script and that there are no other exes in the same directory.
 #   Do not rename the AppFabric Cumulative Update!
 # By:              Ivan Josipovic, 
 $patchfile = Get-ChildItem | where {$_.Name.ToLower().StartsWith("appfabric1.1") -and $_.Extension -eq ".exe"};
 if($patchfile -eq $null) { 
  Write-Host "Unable to retrieve the file.  Exiting Script" -ForegroundColor Red;

 Write-Host "Stopping AppFabric" -ForegroundColor Magenta;
 Stop-SPDistributedCacheServiceInstance -Graceful
 Write-Host "Stopping AppFabric complete" -ForegroundColor green;

 Write-Host "Patching now keep this PowerShell window open" -ForegroundColor Magenta;
 Start-Process -FilePath $patchfile.FullName -ArgumentList "/passive" -Wait;
 Write-Host "Patch installation complete" -foregroundcolor green;

 Write-Host "Updating AppFabric config" -ForegroundColor Magenta;
 $location = "C:\Program Files\AppFabric 1.1 for Windows Server\DistributedCacheService.exe.config"
 $xml = [xml](get-content $location);

 if ($xml.configuration.appSettings.add -eq $null) {
  $appsettings = $xml.CreateElement("appSettings");
  $add = $xml.CreateElement("add");
  $key = $xml.CreateAttribute("key");
  $key.InnerText = "backgroundGC";
  $add.Attributes.Append($key) | out-null;
  $value = $xml.CreateAttribute("value");
  $value.InnerText = "true";
  $add.Attributes.Append($value) | out-null;
  $appsettings.AppendChild($add) | out-null;
  $configsections = $xml.configuration.configSections;
  $xml.configuration.InsertAfter($appsettings,$configsections) | out-null;
 Write-Host "Updating AppFabric config complete" -ForegroundColor green;

 Write-Host "Starting AppFabric" -ForegroundColor Magenta;
 $instance = Get-SPServiceInstance | ? {$_.TypeName -eq "Distributed Cache" -and $_.Server.Name -eq $env:computername};
 Write-Host "Starting AppFabric complete" -ForegroundColor Magenta;