【问题标题】:PowerShell update connection stringsPowerShell 更新连接字符串
【发布时间】:2010-11-19 20:50:28
【问题描述】:

我有以下脚本,它将更新开发中使用的三个不同配置文件的连接字符串。

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

}

主要

问题是连接字符串需要包含 &quote;但是当配置文件被保存时,这变成了 &quote;因此连接字符串不再有效。

有谁知道这样做的方法,我考虑过对文件进行文本替换,但也许有更简洁的方法。

感谢您的帮助。

【问题讨论】:

    标签: linq entity-framework powershell connection-string app-config


    【解决方案1】:

    这一行解决了我的问题:

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

    它将确保将“”写入配置文件并允许应用程序连接到数据库。

    【讨论】:

    • 非常感谢您回来回答您自己的问题 - 它可以帮助我们这些寻找相同内容的人!
    【解决方案2】:

    对于嵌入在双引号分隔的属性值中的双引号,这是预期的。任何机会你都可以使用单引号,例如'e; (或者那是'e:)?

    此外,您可以简化将 XML 加载到的方式:

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

    【讨论】:

    • 感谢 Keithm,我将按照您的建议简化配置文件的加载。我尝试使用单引号,但这不起作用,因为配置随后无效。它与 linq 实体框架一起工作,所以这就是我需要的原因。在连接字符串中。
    【解决方案3】:

    我认为您应该使用 System.Configuration 程序集访问连接字符串。我写了一篇关于如何在构建项目时使用 powershell 加密连接字符串的帖子:Encrypt App.config section using PowerShell as a Post-build event。您必须调整它,因为您的目标不是加密,但它应该对您有所帮助。

    【讨论】:

    • 谢谢菲利普,我会试试这个,让你知道我的进展情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多