Modify web.config with powershell

让人想犯罪 __ 提交于 2019-12-01 16:38:33

I'd to the following

$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"

You can try :

$xml = [xml](Get-Content c:\temp\web.config)
$conString = $xml.connectionStrings.add[0].connectionString
$conString2 = $conString -replace '192.168.1.100','10.10.10.10'
$xml.connectionStrings.add[0].connectionString = $conString2
$conString = $xml.connectionStrings.add[1].connectionString
$conString2 = $conString -replace '192.168.1.100','10.10.10.10'
$xml.connectionStrings.add[1].connectionString = $conString2
$xml.Save('c:\temp\web2.config')

This do the job for the two connection strings. If you don't want to hard code the old IP address you can use :

$conString -replace 'Server=.*;','Server=10.10.10.11;'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!