4

I have the following script that will update the connection string across three different config files used in development.

function Main()
{   
pushd
cd ..
$aomsDir = pwd
$configFiles = @( 'util\GeneratePocos.exe.config', 'src\Web.UI\web.config', 'src\Data\App.Config')

$MyAOMSEntitiesConnStr = $env:AOMS_CONN_STR

Write-Host 'Beginning update of Connection strings...'

foreach($file in $configFiles)
{
    $config = New-Object XML
    $config.Load("$aomsDir\$file")

    foreach($conStr in $config.configuration.connectionStrings.add)
    {
        if($conStr.name -ieq 'AOMSEntities')
        {
            $conStr.connectionString = $MyAOMSEntitiesConnStr
        }
    }

    $config.Save("$aomsDir\$file")
}

Write-Host 'Completed updating connection strings for your machine.'

popd

}

Main

The problem is that the connection string needs to include "e; but when the config file is saved this becomes &quote; As a result the connection string is no longer valid.

Does anyone know of a way to do this, I thought about doing a text replace of the file but maybe there is a cleaner way.

Thanks for your help.

3 Answers 3

3

This line solves my problem:

(Get-Content "$aomsDir\$file") | % {$_ -replace '"', '"'} | Set-Content -path "$aomsDir\$file"

It will ensure that " is written to the config file and allows the application to connect to the database.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for coming back to answer your own question - it helps those of us looking for the same thing!
1

That would be expected for a double quote embedded in a double quote delimited attribute value. Any chance you could use a single quote instead e.g. 'e; (or is that 'e:)?

Also, you can simplify the way you load the XML to:

[xml]$config = Get-Content "$aomsDir\$file"

1 Comment

Thanks Keithm I'll simplify the loading of the config file as you suggested. I tried using a single quot but this didn't work as the configuration was then invalid. It's working with the linq entity framework so that's whay I need " in the connection string.
0

I think you should access the connection strings using the System.Configuration assembly. I wrote a post on how to encrypt connection strings when building a project, using powershell: Encrypt App.config section using PowerShell as a Post-build event. You will have to adapt it since your goal is not to encrypt, but it should help you.

1 Comment

Thanks Philippe I'll try this and let you know how I get on.

Your Answer

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