【发布时间】:2019-09-17 21:50:19
【问题描述】:
我正在尝试使用 PowerShell 使用新值自动更新 web.config 文件中的数据源。
这是我的连接字符串:
<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 脚本:
$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
运行上述脚本时出现以下错误。
正则表达式模式“Data Source=(local)\abc; Trusted_Connection=True; 持久安全信息=真;征募=真;初始目录=abcLogDB" 无效。 在行:5 字符:1 + $value = $value -replace "$oldstring","$newstring" |设置内容-Pas ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ("Data Source=(l...og=OnityLogDB" :String) [], RuntimeException + FullyQualifiedErrorId : InvalidRegularExpression【问题讨论】:
-
无法复制。您发布的代码不会引发您声称的错误。
-
我仍然有同样的问题,我无法用上面的脚本更新我的连接字符串
-
我用您发布的数据样本测试了您发布的代码,它不会抛出您问题中显示的错误。您是否尝试过使用干净的 PowerShell 实例(从 CMD 运行
powershell.exe -NoProfile -NoExit)?那里也有问题吗?
标签: xml string powershell connection