8

I created a little PowerShell script to change connection string in my web.config.

param([string]$webConfigPath, [string]$connectionStringName, [string]$connectionStringValue)

# get the full path of the web config file
$webConfigFile = [IO.Path]::Combine($webConfigPath, 'Web.config')
# load the XML
$webConfig = [xml](cat $webConfigFile)

#change the appropriate config
$webConfig.configuration.connectionStrings.add | foreach {
    if($_.name -eq  $connectionStringName){
        $_.connectionString = $connectionStringValue
    }
}

#save the file
$webConfig.Save($webConfigFile)

I added it to my build process. How to pass the build's variables to the script?

(I use the new script based build process, so I only have a builtin "Arguments" field for the parameter)

2
  • Altering your web.config should happen during the release process, not the build process. Commented Mar 21, 2016 at 21:55
  • I use the build process to put my site to the two test servers on every checkin. How to do that without build process? Commented Mar 21, 2016 at 21:57

1 Answer 1

7

You can put all parameters in a single line into Arguments files like this:

-webConfigPath "c:\web.config" -connectionStringName "My connection string"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I have one more question. How to pass the variables of the build process? Like BuildConfiguration and my custom attributes.
With an $Env:BUILD_BUILDNUMBER syntax from the source code. Oh, I was stupid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.