I am trying to automate to update the data source in a web.config file with new values using PowerShell.
Here is my connection string:
<connectionStrings>
<add name="abcLogging" connectionString="Data Source=(local)\abc; Trusted_Connection=True; Persist Security Info=True; Enlist=true; Initial Catalog=abcLogDB" providerName="System.Data.SqlClient" />
</connectionStrings>
PowerShell script:
$newstring = '"Data Source=(test)\abc; Trusted_Connection=True; Persist Security Info=True; Enlist=true; Initial Catalog=abcLogDB" '
$oldstring = '"Data Source=(local)\abc; Trusted_Connection=True; Persist Security Info=True; Enlist=true; Initial Catalog=abcLogDB" '
$XmlDocument = [xml](Get-Content "D:\abc\Web.config");
$value = $XmlDocument.configuration.connectionStrings.add.connectionstring
$value = $value -replace "$oldstring","$newstring" | Set-Content -PassThru
I have got the following error when I ran the above script.
The regular expression pattern "Data Source=(local)\abc; Trusted_Connection=True;
Persist Security Info=True; Enlist=true; Initial Catalog=abcLogDB" is not valid.
At line:5 char:1
+ $value = $value -replace "$oldstring","$newstring" | Set-Content -Pas ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ("Data Source=(l...og=OnityLogDB" :String) [], RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression
powershell.exe -NoProfile -NoExitfrom CMD)? Does the problem occur there as well?