【问题标题】:Scripting setup of database mail数据库邮件的脚本设置
【发布时间】:2010-08-19 11:51:37
【问题描述】:

我已经使用 SQL Server 2008 GUI 在我的测试服务器上设置了数据库邮件配置文件和帐户,现在我想将它们复制到我们的生产数据库中。

有没有办法生成一个脚本来做到这一点?

【问题讨论】:

    标签: sql-server-2008 email database-mail


    【解决方案1】:

    AFAIK,没有办法必须从 SSMS 编写脚本,但您可以在 TSQL 中创建一个可传输的脚本,然后在所有服务器上重复使用它。这是一个很好的例子,可以帮助您开始:

    USE [master]
    GO
    sp_configure 'show advanced options',1
    GO
    RECONFIGURE WITH OVERRIDE
    GO
    sp_configure 'Database Mail XPs',1
    GO
    RECONFIGURE 
    GO
    -- Create a New Mail Profile for Notifications
    EXECUTE msdb.dbo.sysmail_add_profile_sp
           @profile_name = 'DBA_Notifications',
           @description = 'Profile for sending Automated DBA Notifications'
    GO
    -- Set the New Profile as the Default
    EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
        @profile_name = 'DBA_Notifications',
        @principal_name = 'public',
        @is_default = 1 ;
    GO
    -- Create an Account for the Notifications
    EXECUTE msdb.dbo.sysmail_add_account_sp
        @account_name = 'SQLMonitor',
        @description = 'Account for Automated DBA Notifications',
        @email_address = 'email@domain.com',  -- Change This
        @display_name = 'SQL Monitor',
        @mailserver_name = 'smtp.domain.com'  -- Change This
    GO
    -- Add the Account to the Profile
    EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
        @profile_name = 'DBA_Notifications',
        @account_name = 'SQLMonitor',
        @sequence_number = 1
    GO
    

    另一种选择是利用 SMO,通过 .NET 或 powershell 生成脚本。对此的 SMO 参考是:

    SqlMail Class

    更新:

    事实证明,使用 Powershell 和 SMO 编写脚本是多么容易:

    [void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");
    
    #Set the server to script from
    $ServerName = "ServerName";
    
    #Get a server object which corresponds to the default instance
    $srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server $ServerName
    
    #Script Database Mail configuration from the server
    $srv.Mail.Script();
    

    【讨论】:

    • 乔纳森 - 你这个笛卡尔加入/炸弹!
    • 这是一个了不起的发现!在 TSQL 方面,如果您希望将帐户自定义为每个服务器的名称,例如 donotreply_FooBarSQL@mycompany.com,您可以使用@@SERVERNAME 子输入某些参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多