Powershell修改Web.config mailSettings

标签 powershell web-config

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

<system.net>
    <mailSettings>
        <smtp from="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbbaabcbb8fbbaabcbbe1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>">
            <network host="mailtrap.io" userName="masked" password="masked" port="2525" enableSsl="false" />            
        </smtp>
    </mailSettings>
</system.net>

为此进行生产:

<system.net>
    <mailSettings>
        <smtp from="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c7c5d0c5c8ddd7d0e4c0c5d0c5c2cdcac7cc8ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>">
            <network host="pod51010.outlook.com" userName="someuser" password="somepassword" port="587" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>

我的 powershell 经验为零,我不知道从哪里开始。谁能提供一些见解?

最佳答案

我明白了:

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

关于Powershell修改Web.config mailSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957949/

相关文章:

powershell - 在PowerShell中声明包含另一个变量名称的变量(名称)

powershell - 如何使用帐户 key 从 PowerShell 列出 Azure Batch 池?

.net - 是否可以使用属性初始化 .NET 类型?

c# - 可以模仿我自己的配置文件的 web.config 重启行为吗?

c# - 无法加载文件或程序集“MySql.Web”

powershell 中的日期/时间条件 - SharePoint - LastItemModifiedDate - LastRunTime

.net - PowerShell:一种创建闭包的优雅方式

file-upload - .NET Core 3 和 IIS Express maxAllowedContentLength 不起作用

visual-studio-2015 - Visual Studio 2013/2015/2017 探查器在 web.config 中添加了不可发布的行?

azure - 在服务配置中设置应用程序 ConnectionString 而不是 Azure 中的 web.config