使用Powershell更新多个连接字符串数据源

时间:2015-03-13 18:15:37

标签: xml powershell connection-string config

因此,我在互联网上看到了一个非常直接的演示,以更新我的SQL连接字符串上的data-source属性。但是,这一行最初将错误抛到了

之下
$doc = (gc $file) -as [xml]
  

gc:无法找到路径' C:\ Windows \ system32 \ LVOAGT.exe.config'因为它不存在。

由于我是Powershell的新手,我删除了gc,错误消失了......但是现在$doc变量是空白的。知道如何获取配置文件并使用下面的方法只更新我的连接字符串的data source部分?

#environment variables
$env = "DEV"                       #This will need to be changed from DEV / INT / QUA / PRD
$oldServer = "QUASQ03"          #This will need to be changed from DEV / INT / QUA / PRD

#file and folder variables
$directory = "D:\AMS"
$folders = @("AgentsMonetToDss")

#new database value
$newValue = "$env-AgentResources-AMS-SQL.CORP"

#pull config file and insert new database connection
foreach($folder in $folders)
{
    Write-Host "Updating app.config for $folder" -ForegroundColor Yellow
    $dir = Get-ChildItem $directory\$folder -Recurse
    $config = $dir  | where {$_.extension -eq ".config"}

    foreach($file in $config)
    {
        $doc = (gc $file) -as [xml]
        $root = $doc.get_DocumentElement();
        $newCon = $root.connectionStrings.add.connectionString.Replace("data source=$oldServer\sql08a", "data source=$newValue\sql08a")
        $root.connectionStrings.add.connectionString = $newCon
        $doc.Save($file)
    }
}

修改

以下是foreach变量中内部$file循环的第一次迭代中包含的内容的屏幕截图。弹出窗口中的信息准确无误(文件名,目录)

enter image description here

第二次编辑

因此下面的代码将更新连接字符串。但是这一行给出了下面的错误。

$root.connectionStrings.add.connectionString = $newCon
  

属性' connectionString'在这个对象上找不到。验证该属性是否存在且可以设置。

    $doc = [xml](Get-Content $file.FullName)
    $root = $doc.get_DocumentElement();
    [string]$newCon = $root.connectionStrings.add.connectionString.Replace($oldServer, $newValue)
    $root.connectionStrings.add.connectionString = $newCon
    $doc.Save($file.FullName) 

第三次编辑

错误'connectionString' cannot be found on this object似乎是因为我们在<connectionStrings>节点中有多个连接字符串。如果我从配置中删除其中一个连接字符串,那么脚本运行正常。所以我想问题是如何使用PowerShell更新MULTIPLE连接字符串?问题似乎是以下几行

$newCon = $root.connectionStrings.add.connectionString.Replace($oldServer, $newValue)
$root.connectionStrings.add.connectionString = $newCon

第一行将搜索两个连接字符串并根据请求替换值。但是,由于我在$newCon中只有一个长字符串,并且我试图添加connectionString $root属性,因此发生了冲突。

2 个答案:

答案 0 :(得分:0)

它很难看,但它有效......

我最终必须将整个连接字符串存储在变量中,并对-contains属性运行connectionString检查。如果任何人都有一个非常有效的解决方案,请将这篇文章保持开放一段时间。

#generated config connection strings
Write-Host "Updating config ConnectionString nodes" -ForegroundColor Green
foreach($folder in $folders)
{
    Write-Host "Updating config for $folder" -ForegroundColor Yellow
    $dir = Get-ChildItem $directory\$folder -Recurse 
    $config = $dir  | where {$_.extension -eq ".config"}

    foreach($file in $config)
    {       
        $doc = [xml](Get-Content $file.FullName) 
        $doc.configuration.connectionStrings.add |%{
            if($_.connectionString.ToLower() -contains $oldGenConn.ToLower()){
                $_.connectionString = $newGenConn
            }            
        }
        $doc.Save($file.FullName)
    }
} 

答案 1 :(得分:0)

假定您的.exe.config文件中的连接字符串如下:

  <connectionStrings>
    <add name="DbConnectionExternal" connectionString="DbConnectionExternal" />
    <add name="DbConnectionSecurity" connectionString="DbConnectionSecurity" />
    <add name="ErrorDb" connectionString="ErrorDb" />
    <add name="OracleConnection" connectionString="OracleConnection" />
  </connectionStrings> 

执行以下脚本文件后,给定路径的根文件夹中存在的所有.exe.config文件中的此连接字符串将更新为:

  <connectionStrings>
    <add name="DbConnectionExternal" connectionString="A" />
    <add name="DbConnectionSecurity" connectionString="B" />
    <add name="ErrorDb" connectionString="C" />
    <add name="OracleConnection" connectionString="D" />
  </connectionStrings> 

脚本文件:

get-childitem "Include the file path of your root folder" -recurse | where {$_.Name -match ".exe.config"} | % {
         $appConfigFile = $_.FullName
    $appConfig = New-Object XML
    $appConfig.Load($appConfigFile)
    foreach($connectionString in $appConfig.configuration.connectionStrings.add){
            if($connectionString.name -eq 'DbConnectionExternal'){
        $connectionString.connectionString = 'A'
        }
            elseif($connectionString.name -eq 'DbConnectionSecurity'){
        $connectionString.connectionString = 'B'
        }
            elseif($connectionString.name -eq 'ErrorDb'){
        $connectionString.connectionString = 'C'
        }
            elseif($connectionString.name -eq 'OracleConnection'){
        $connectionString.connectionString = 'D'
        }
    }
    $appConfig.Save($appConfigFile)
    }

它运行完美,当您将应用程序部署到服务器中时,此脚本将非常有用。您不需要一个个地更新每个配置文件。