Useful Powershell Snippets When Implmenting A Continuous Deployment Process With Octopus Within Episerver

This post is a quick reference sheet for some useful powershell scripts I've used in the past. These scripts are all designed to help you to pragmatically update your config files using powershell. I fully admit the contents of this post won't be for every reader. Before using any of these, I would recommend reading this post first, 4 Points You Should Consider Before Using Octopus With Episerver. As a general bit of advice, I would recommend, as a first port of call, using web config transforms. However, some clients can have non-standard infrastructure set-up, crazy security processes, and God knows what else. In most of the snippets below, these powershell snippets are being run by the build server before Octopus/the deployment process is run. In most of the snippets, I'm getting a config file from the build server, replacing the value with an Octopus variable name only. This code isn't injecting config values directly, however, you could change the Octopus variable to use a pre-defined value.. it's all up to you. In this set-up when Octopus gets triggered, it will then replace the variables with whatever's set in your variable. In this set-up you're effectively doing two transforms. one to programmatically add the Octopus variables into the config files and using Octopus afterwards to do the transforms on the variables themselves. A bit long winded I'll happily admit and I wouldn't class this process as 'standard', or 'good practice'. One benefit of this approach, however, is that IT administrators and people who may not have Visual Studio installed, can work on your build process and procedure.

Load and Save A Config File

    $webConfigFile = 'web.config'
    $webconfig = New-Object XML
    $webconfig .Load($webFile)
    
    $webconfig .Save($webconfig )
This first snippet will be the starting point for most of the other snippets below. This snippet shows how you can load a config file with powershell and then save it afterwards.

Update An AppSetting

    $appSettingsFile = 'appSettings.config'
    $appSettings = New-Object XML
    $appSettings.Load($appSettingsFile )
    
    foreach ($node in $appSettings .SelectNodes("/appSettings/add[@key='EPi.DebugView.Enabled']")) {
        $node.value = 'true'
    }
This one will find an app settings in a seperate app settings config file and change the 'EPi.DebugView.Enabled' setting to true.

Change The Episerver Log File Location

    $logFile = 'EPiServerLog.config'
    $log = New-Object XML
    $log.Load($logFile)

    foreach ($node in $log.SelectNodes("/log4net/appender/file")) {
        $ans = $node.value -replace 'C\:\\Logs','#{OctopusVariableName}'
        $node.value = $ans
    }
    
    $log.Save($logFile)

Change The App Data Folder Location

    $EPiServerFrameworkFile = 'EPiServerFramework.config'
    $EPiServerFramework= New-Object XML
    $EPiServerFramework.Load($EPiServerFrameworkFile )
    
    foreach ($node in $EPiServerFramework.SelectNodes("/episerver.framework/appData")) {
        $node.basePath = '#{OctopusVariableName}'
    }
    
    $framework.Save($frameworkFile)

Update All Rewrite Rules Url

foreach ($node in $rewriteWeb.SelectNodes("/rules/rule/action")) {
  $propertyExists = $node.url

  if ($propertyExists -ne $null) {
     $action = $node.url -replace 'http://localhost','#{OctopusVariableName}'
     $node.url = $action
   }
}
This script will update any of your URL rewrite rules Url properties with an Octopus variable. This came in useful on a project that required a reverse proxy to handle legacy resources.

Update Redis In Your ConnectionString

    $webConfigFile = 'web.config'
    $webconfig = New-Object XML
    $webconfig .Load($webFile)
    foreach ($node in $webconfig .SelectNodes("/configuration/system.web/sessionState/providers/add")) {
        $node.host = '#{RedisConnectionString}'
        $node.port = '#{RedisPort}'
        $node.accessKey = '#{RedisPassword}'
        $node.ssl  = '#{RedisSSLEnabled}'
    }
This script will set your Redis session state, to use Octopus variables instead.

Change The Episerver Indexing Service URL

    foreach ($node in $webconfig .SelectNodes("/configuration/episerver.search/namedIndexingServices/services/add")) {
        $node.baseUri = '#{OctopusVariableName}'
    }
As the name suggests, this will update the baseUri property for the Episerver indexing service, to replace it with an Octopus variable.

Update The Episerver License File To Use An Octopus Variable

    $licenceFile = 'License.config'
    '#{EPiServerLicence}' | Out-File $licenceFile 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top