使用Powershell修改web.config [英] Modify web.config with powershell

查看:103
本文介绍了使用Powershell修改web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to try to update a web.config file to change the IP address only of the web.config I have included the section of code Im looking at for powershell to script the change.

<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地址即可。

I would like a very simple solution to this just update the ServerIP address.

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

Anyone know an easy way to do this with 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");

如果您只是想替换特定的连接字符串,则可以这样获取:

If you are just looking to replace a specfic connection string, I'd fetch it like this:

$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修改web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆