Share via


Powershell: Add connection string to web.config

Question

Tuesday, January 8, 2019 5:07 PM

Dear all,

I have a connection string that looks like this

<add name="MyConn" connectionString="metadata=res://*/Products.csdl|res://*/Products.ssdl|res://*/Products.msl;provider=System.Data.SqlClient;provider connection string="data source=100.6.8.111;initial catalog=myDB;user id=sa;multipleactiveresultsets=True"" providerName="System.Data.EntityClient"/> 

I want to add this connection string in the  <connectionStrings> in web.config

Any help would be greatly appreciated

All replies (7)

Tuesday, January 8, 2019 6:17 PM

Use the commandline IIS web configuration utility.  "AppCmd.exe"

/en-us/iis/get-started/getting-started-with-iis/getting-started-with-appcmdexe

Post IIS configuration issues in the IIS forum for best help. http://forums.iis.net

\(ツ)_/


Tuesday, January 8, 2019 6:29 PM

Thanks for your reply. But it has to be done through PowerShell Script


Tuesday, January 8, 2019 6:33 PM

Post in IIS forum liked above.  They will show you how.

See the following: /en-us/powershell/module/webadministration/add-webconfiguration?view=win10-ps

\(ツ)_/


Tuesday, January 8, 2019 6:36 PM

This may be more helpful:

http://britv8.com/powershell-iis-set-connection-string-in-web-config-solution/

\(ツ)_/


Wednesday, January 9, 2019 8:28 PM

ok I tried something like this

$webConfig = '.\web.config'
[xml]$web =  (Get-Content $webConfig)
$add=$web.CreateElement('Add')
$parent=$web.SelectSingleNode('//connectionStrings')
$web.Save($webConfig)

Actually after getting node connectionStrings, I don't know how add the connection string. Any help would be greatly appreciated.


Wednesday, January 9, 2019 8:38 PM

Good start.  Remember that all strings in ML are case sensitive.,

$webConfig = '.\web.config'
[xml]$web = Get-Content $webConfig

$element = $web.CreateElement('add') # lower case
$node = $web.SelectSingleNode('//connectionStrings')
$node.AppendChild($element)

$web.Save($webConfig)

\(ツ)_/


Tuesday, January 22, 2019 7:48 AM

Hi,

Was your issue resolved?

If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

If no, please reply and tell us the current situation in order to provide further help.

Best Regards,

Lee

Just do it.