PowerShell updates connection strings

I have the following script that will update the connection string in three different configuration 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

}

home

The problem is that the connection string must include & quote; but when the configuration file is saved, it becomes & quote; As a result, the connection string is no longer valid.

Does anyone know a way to do this, I thought about making a text replacement for the file, but there may be a cleaner way.

Thank you for your help.

+3
source share
3 answers

This line solves my problem:

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

, " .

+3

, . , , , , "; ( " e:)?

, XML:

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

, , System.Configuration. , , powershell: App.config PowerShell . , - , .

0

Source: https://habr.com/ru/post/1715227/


All Articles