Search code examples
powershellencryptionoctopus-deploy

Octopus Post Deploy Encrypt Application Settings


Does anyone know if there is a tutorial showing how to do encryption of ApplicationSettings in PostDeploy.ps1 when using Octopus for deployment? Or can just show me an example of how the powershell script should look?

EDIT:

Here's an example of the App.config area I want encrypted:

 <?xml version="1.0"?>
  <configuration>
    <applicationSettings>
        <SmsSurveysWeb.Properties.Settings>
            <setting name="WebSiteUrl" serializeAs="String">
                <value>http://myurl</value>
            </setting>
            <setting name="SmsSurveysConnectionString" serializeAs="String">
               <value>Database=MyDatabase;Data Source=MySource;User ID=MyUser;Password=MyPassword;Application Name=My Application;</value>
            </setting>
        </SmsSurveysWeb.Properties.Settings>
    </applicationSettings>
 </configuration>

Solution

  • I figured it out. I needed to use the OpenMappedExeConfiguration method.

    $configurationFileMap = New-Object -TypeName System.Configuration.ExeConfigurationFileMap
    $configurationFileMap.ExeConfigFilename = ".\Web.config"
    
    $c =[System.Configuration.ConfigurationManager]::OpenMappedExeConfiguration($configurationFileMap, [System.Configuration.ConfigurationUserLevel]"None")
    $s=$c.GetSection("applicationSettings/ProjectName.Properties.Settings")
    
    $s.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
    $c.Save()