【问题标题】:How to update the Data Source connection string using PowerShell in the XML file如何在 XML 文件中使用 PowerShell 更新数据源连接字符串
【发布时间】: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


【解决方案1】:

欢迎来到 StackOverflow,Hari Krishna!您的脚本发生了一些事情。

  1. 旧字符串和新字符串中的引号过多 - 将找不到旧字符串。一般来说,应该使用单引号,除非有一个嵌入的$variableName 用它的值来扩展。

  2. 括号被视为正则表达式特殊字符,因为您使用的是-replace 语法。 [string] 对象的.Replace 方法可用于没有正则表达式的简单替换。

  3. Set-Content 命令不知道要对哪个文件进行操作。但是您并不想将文件的内容设置为仅$value。这将为您提供一个没有任何 XML 内容而只有连接字符串的文件。

这是一个应该做你想做的脚本:

$configFile = 'C:\temp\web.config'

Set-Content -Path $configFile -Value @"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
</configuration>
"@

$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 $configFile);
$XmlDocument.configuration.connectionStrings.add | %{
    If($_.connectionString -eq $oldstring){
        $_.connectionString = $newstring
    }
}
$XmlDocument.Save($configFile)
Get-Content -Path $configFile

编辑:更正了在附加测试期间发现的问题。

【讨论】:

  • 感谢 Rich 的详细回复,我收到以下错误,无法转换参数“0”,其值为:“abc\Web.config”,用于“WriteContentTo”键入“System. Xml.XmlWriter": "无法将 "System.String" 类型的 "D:\abc\Web.config" 值转换为 "System.Xml.XmlWriter" 类型。"在 line:6 char:1 + $XmlDocument.WriteContentTo($ConfigFile) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
  • 当我添加 $XmlDocument.Save($ConfigFile) 而不是 $XmlDocument.WriteContentTo($ConfigFile) 时,没有错误,但是文件中的值没有得到更新
  • 这很奇怪。您使用的是哪个版本的 Powershell?尝试在 Powershell 提示符下运行 $PSVersionTable 并回复结果。我已经在 PSVersion 4 中成功测试了上面的脚本。
  • 我的 5.1 版 PowerShell
  • 您是否复制了上面的脚本完全按照所写的?错误消息表明$configFile 的值不正确。另外,我同意 @Ansgar-Wiechers 的建议,尝试使用新的 Powershell 窗口。
猜你喜欢
  • 1970-01-01
  • 2010-11-19
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
相关资源
最近更新 更多