So I'm following what seemed like a pretty straight-forward demo on the internet to update the data-source
attribute on my SQL connection string. However, this line originally threw the error below
$doc = (gc $file) -as [xml]
gc : Cannot find path 'C:\\Windows\\system32\\LVOAGT.exe.config' because it does not exist.
Since I'm brand new to Powershell I removed the gc
and the error went away... however now the $doc
variable is blank. Any idea how to grab a config file and use the method below to just update the data source
portion of my connection string?
#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)
}
}
EDIT
Here is a screenshot of what's contained in the first iteration of the inner foreach
loop in the $file
variable. The information in the pop up is accurate (filename, directory)
SECOND EDIT
So the following code below will update the connection string. However this line gives the error below.
$root.connectionStrings.add.connectionString = $newCon
The property 'connectionString' cannot be found on this object. Verify that the property exists and can be set.
$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)
THIRD EDIT
The error 'connectionString' cannot be found on this object
appears to be because we have multiple connection strings in the <connectionStrings>
node. If I remove one of the connection strings from the config then the script runs fine. So I guess the question is how do I update MULTIPLE connection strings using powershell? The issue seems to be these lines below
$newCon = $root.connectionStrings.add.connectionString.Replace($oldServer, $newValue)
$root.connectionStrings.add.connectionString = $newCon
The first line will scour both connection strings and replace the value as is requested to do. However, since I have simply a long string in $newCon
and I'm attempting to add to the connectionString
property of $root
there's a clash.
It's ugly but it worked...
I eventually had to store the entire connection string in a variable and run a -contains
check against the connectionString
property. Will leave this post open for awhile in case anyone has a much efficient solution.
#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)
}
}
Assume your connection string in your .exe.config files are as follows:
<connectionStrings>
<add name="DbConnectionExternal" connectionString="DbConnectionExternal" />
<add name="DbConnectionSecurity" connectionString="DbConnectionSecurity" />
<add name="ErrorDb" connectionString="ErrorDb" />
<add name="OracleConnection" connectionString="OracleConnection" />
</connectionStrings>
After executing the following script file, this connection string inside all the .exe.config files which exists inside your root folder of the given path will be updated to:
<connectionStrings>
<add name="DbConnectionExternal" connectionString="A" />
<add name="DbConnectionSecurity" connectionString="B" />
<add name="ErrorDb" connectionString="C" />
<add name="OracleConnection" connectionString="D" />
</connectionStrings>
Script file:
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)
}
It works perfectly and this script will be very useful when you deploy your application in to a server. You do not need to update each and every config file one by one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.