As a complement to the previous article on Extending EPiServer MSBuild config transforms to provide server specific configuration for TCP EventReplication and Licenses I wrote a small Windows PowerShell script example on how you may copy the proper configuration files to the proper server automatically during deploy. This script is available at GitHub.
Copying files with PowerShell based on environment computer name and regex
The first thing that happens in the PowerShell script examle is gathering up a few things. The name of the current server may be retrieved by accessing computername from the evironment variable $env (3). You may want to respecify where you keep your config files (4), and where your webroot is (5), for your environment; depending on how you do your deploys.
$currentServer = $env:computername $pathToConfigs = ".\source" $pathToWebRoot = ".\target"
Get-ChildItem is used to retrive a list of all the files in the specified configuration directory, and the Force flag ensures that hidden files (as well as system files) are also included; in other words, if you don’t need it, it could probably be omitted. The resulting list of files is then passed on to Where-Object that looks at the Name parameter of each object and includes only those that contain the name of the current machine. Match is case insensitive in PowerShell.
$currentServerConfigFiles = Get-ChildItem $pathToConfigs -Force | Where-Object {$_.Name -match $currentServer}
Looping through the list of relevant configuration files for the current server the script then extracts the filename from each file object (9). The target filename is then constructed by removing the name of the current server from the source filename (10) replacing it with an empty string; the Replace operation is also case insensitive in PowerShell.
foreach ($sourceFile in $currentServerConfigFiles) { $filename = $sourceFile.ToString() $targetFile = $filename -Replace "$currentServer.", ""
As we only have the names of the files, we concatenate a source and a target path that is passed on to Copy-Item for copying the file; the Force flag ensures that the target file is overwritten if there already is one in it’s location.
$source = "$pathToConfigs\$filename" $target = "$pathToWebRoot\$targetFile" Copy-Item -Force -Path $source -Destination $target echo "Copied config file '$source' to '$target'." }
What we are actually doing is to have a set of config file transformations and applying them to config files (through VS solution configurations) while preparing release for particular server. Wouldn’t be a bit easier?
Sure, I guess you could build a separate deploy package for each server, but once you start to have like 3 or 4 different machines I’d prefer having only one package; not having to think about using the correct one.
In this particular set up we keep one PowerShell script for constructing the deploy package (through a similar server specific MSBuild transform as described in the referenced article), and one script for deploying the proper installation depending on which server it’s being run on; so we get different config and behaviour depending on machine.