Modify web.config with 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.

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

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

Anyone know an easy way to do this with PowerShell.





I’d to the following

$cfg = [xml](gc web.config)
# Replace all references of the IP in all connection string
   $_.connectionString = $_.connectionString -replace "", "";

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

$con= $cfg.configuration.connectionStrings.add|?{$ -eq "SqlDataCon"};
# Replace the content
$con.connectionString = $con.connectionString -replace "", ""


Leave a Reply

Your email address will not be published. Required fields are marked *