Chris Kooken
Chris Kooken

Reputation: 33938

Powershell Modify Web.config mailSettings

I am using RedGate's deployment manager for our deployments. I need to use a powershell script as part of the deployment to change this:

<system.net>
    <mailSettings>
        <smtp from="[email protected]">
            <network host="mailtrap.io" userName="masked" password="masked" port="2525" enableSsl="false" />            
        </smtp>
    </mailSettings>
</system.net>

To this for production:

<system.net>
    <mailSettings>
        <smtp from="[email protected]">
            <network host="pod51010.outlook.com" userName="someuser" password="somepassword" port="587" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>

Having zero experience in powershell, I'm not sure where to begin. Can anyone offer some insight?

Upvotes: 2

Views: 892

Answers (1)

Chris Kooken
Chris Kooken

Reputation: 33938

I figured it out:

 #Set the Connection String and the path to web.config (or any config file for that matter)
 $webConfigPath = "web.config"

 # Get the content of the config file and cast it to XML and save a backup copy labeled .bak followed by the date
 $xml = [xml](get-content $webConfigPath)     

 $root = $xml.get_DocumentElement();

 $root."system.net".mailSettings.smtp.network.host = $mailHost
 $root."system.net".mailSettings.smtp.network.userName = $mailUsername
 $root."system.net".mailSettings.smtp.network.password = $mailPassword
 $root."system.net".mailSettings.smtp.network.port = $mailPort
 $root."system.net".mailSettings.smtp.network.enableSsl = $mailEnableSSL

 # Save it
 $xml.Save($webConfigPath)

Upvotes: 3

Related Questions