Skip to content

Instantly share code, notes, and snippets.

@zachbonham
Last active August 29, 2015 14:00
Show Gist options
  • Save zachbonham/11303600 to your computer and use it in GitHub Desktop.
Save zachbonham/11303600 to your computer and use it in GitHub Desktop.
More fun with PowerShell string expansion for .NET app/web.config
<#
Usage:
ApplyConfig "app.config" "DEV"
$configFile - the app/web.config we are 'transforming'.
$environment - the environment context we are transforming for. Maybe snarf this from <appSettings><add key="Environment" value="some value"/>?
#>
param($configFile, $environment="LOCAL")
$workingDirectory = Split-Path $MyInvocation.MyCommand.Path
# dot sourcing our variable declarations
#
. .\$($environment).tokens.ps1
$configFilePath = join-path $workingDirectory $configFile -resolve
$content = [IO.File]::ReadAllText($configFilePath)
#content contains all of our configuration, with variable expressions
#calling ExpandString will replace those expressions with their values
#
$expandedConfig = $ExecutionContext.InvokeCommand.ExpandString($content)
# uncomment to see values
# $expandedConfig
#set-content with $expandedConfig in output folder
$MyDbConnection="my DEV connection string"
$HostMachineName = "DEV001"
$MyDbConnection="my local connection string"
$HostMachineName = [environment]::machinename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment