Powershell修改Web.config mailSettings

时间:2014-05-30 15:18:10

标签: powershell web-config

我正在使用RedGate的部署管理器进行部署。我需要使用powershell脚本作为部署的一部分来改变它:

<system.net>
    <mailSettings>
        <smtp from="test@test.com">
            <network host="mailtrap.io" userName="masked" password="masked" port="2525" enableSsl="false" />            
        </smtp>
    </mailSettings>
</system.net>

为了生产:

<system.net>
    <mailSettings>
        <smtp from="catalyst@datafinch.com">
            <network host="pod51010.outlook.com" userName="someuser" password="somepassword" port="587" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>

对PowerShell没有经验,我不知道从哪里开始。任何人都可以提供一些见解吗?

1 个答案:

答案 0 :(得分:2)

我明白了:

 #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)