Instantly share code, notes, and snippets.

Embed
What would you like to do?
When Sitecore installs a package, the config files in the package are saved with a different file name. This script will backup the old file and rename the new one.
#*=====================================================================
#* Function: ProcessConfigFiles
#*=====================================================================
#* Author: Charles Turano (Hedgehog Development http://hhogdev.com)
#* Arguments:
#* $WebRoot: Unc path to the server root
#* $PackageFileName: Filename of the package installed on the web server
#*=====================================================================
#* Purpose:
#* When Sitecore installs a package, the config files in the package are
#* saved with a different file name. This script will backup the old file
#* and rename the new one.
#*=====================================================================
function ProcessConfigFiles($WebRoot, $PackageFileName)
{
$WebRoot = $WebRoot.ToLower()
$PackageFileName = $PackageFileName.ToLower()
$PackagePrefix = $PackageFileName.Remove($PackageFileName.LastIndexOf(".update"));
write-host "Looking for files ending with .config.$PackagePrefix"
$UpdatedConfigs = get-childitem -path $WebRoot -Include "*.config.$PackagePrefix" -recurse
#See if there are any files
if ($UpdatedConfigs)
{
foreach($UpdatedConfig in $UpdatedConfigs)
{
$UpdateConfigFilename = $UpdatedConfig.FullName.ToLower();
#Ignore files in /temp folder
if (!$UpdateConfigFilename.StartsWith("$WebRoot\temp"))
{
write-host "Found file: " -nonewline
write-host -ForegroundColor Green $UpdateConfigFilename
$OriginalFileName = $UpdateConfigFilename.Remove($UpdateConfigFilename.Length - ($PackagePrefix.Length + 1))
if (test-path $OriginalFileName)
{
$OriginalFile = get-item $OriginalFileName
#Backup the file
$BackupPostfix = Get-Date -format "yyyyMMdd-hhmmss"
$BackupFileName = $OriginalFileName + ".BAK." + $BackupPostfix
copy-item $OriginalFileName $BackupFileName
#Copy the new config
copy-item $UpdatedConfig $OriginalFileName
#Delete the new config
remove-item $UpdatedConfig
write-host "Backup and replace of " -nonewline
write-host -ForegroundColor Green $OriginalFilename -nonewline
write-host " complete"
write-host
}
else
{
write-host -ForegroundColor Yellow "Can't find file $OriginalFileName"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment