powershell - 使用Powershell修改web.config

标签 powershell web-config connection-string powershell-3.0

我需要尝试更新web.config文件以仅更改web.config的IP地址
我已经包含了代码部分,我正在寻找用于编写更改脚本的Powershell。

<connectionStrings>
    <add name="connectionString" connectionString="provider=SQLOLEDB;Server=192.168.1.100;database=sample;Trusted_Connection=Yes" providerName="System.Data.OleDb" />
    <add name="sqlConnectionString" connectionString="Data Source=192.168.1.100;Initial Catalog=sample;Trusted_Connection=Yes" providerName="System.Data.SqlClient" />
  </connectionStrings>

我想要一个非常简单的解决方案
只需更新ServerIP地址。

任何人都知道使用PowerShell执行此操作的简单方法。

最佳答案

我想要以下

$cfg = [xml](gc web.config)
# Replace all references of the IP in all connection string
$cfg.configuration.connectionStrings.add|%{
   $_.connectionString = $_.connectionString -replace "192.168.1.100", "1.0.0.1";
}
$cfg.Save("Web.config");

如果您只是想替换特定的连接字符串,则可以这样获取:
$con= $cfg.configuration.connectionStrings.add|?{$_.name -eq "SqlDataCon"};
# Replace the content
$con.connectionString = $con.connectionString -replace "192.168.1.100", "1.0.0.1"

关于powershell - 使用Powershell修改web.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827324/

相关文章:

asp.net - web.config 将非 www 或非 https 重定向到 https ://www using rules

c# - 使用系统数据源而不是用户数据源作为 ODBC 连接字符串

windows - Powershell区分大小写重命名

powershell - 为什么 `exit`抑制Select-Xml的错误?

powershell - 我怎么能解析精确星期三2018年6月27日08:50:00 -0500

.net - 无法在 C# web.config 中创建连接字符串

wpf - 如何在 Powershell 中将 rtf 文件加载到 WPF RichTextBox

重定向 https ://to https://www in IIS 8

sql-server - 如何在 ADODB 连接字符串中设置 "Application Name"

entity-framework - 使用 |DataDirectory| 时出现 "URI formats are not supported"异常在 Entity Framework 连接字符串中