用于检查是否在SQL Server中启用了邮件配置文件的脚本

时间:2018-07-03 13:46:47

标签: sql sql-server sql-server-2008

是否有人拥有脚本(TSQL或PowerShell)来检查是否在SQL Server上启用了邮件配置文件

我注意到我们没有收到来自大量生产服务器的邮件,因为未启用邮件配置文件。

我需要一个脚本在所有产品框中运行,以检查是否启用了此功能。我做了一些研究,但是找不到脚本。非常感谢。

enter image description here

2 个答案:

答案 0 :(得分:2)

我在dba.stackexchange.com上也问过类似的问题。

在该帮助下,我创建了this script which I have on GitHub,我设置了一个作业以在更改时提醒我。

这是您似乎关心的部分:

/* 
   Confirm the Database Mail account and profile is configured correctly 
*/ 

DECLARE @DatabaseMail VARCHAR(255);  

SELECT   
    ProfileName = smp.name  
    ,AccountName = sma.name  
    ,AccountFromAddress = sma.email_address  
    ,AccountReplyTo = sma.replyto_address  
    ,SMTPServer = sms.servername  
    ,SMTPPort = sms.port  
FROM msdb.dbo.sysmail_account sma  
    INNER JOIN msdb.dbo.sysmail_profileaccount smpa ON sma.account_id = smpa.account_id  
    INNER JOIN msdb.dbo.sysmail_profile smp ON smpa.profile_id = smp.profile_id  
    INNER JOIN msdb.dbo.sysmail_server sms ON sma.account_id = sms.account_id;

/*  
    Confirm SQL Server Agent is configured to use Database Mail correctly  
*/  
DECLARE @res TABLE  
(  
    Value VARCHAR(255)  
    , Data VARCHAR(255)  
);  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail';  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile';  
IF (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'UseDatabaseMail' AND r.Data = 1  
    ) = 1 AND   
    (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'DatabaseMailProfile' AND r.Data IS NOT NULL  
    ) = 1  
SET @DatabaseMail = 'Configured'  
ELSE  
SET @DatabaseMail = 'Not Configured';  

select @DatabaseMail

或者,关于概要文件是什么以及如果在代理级别启用了概要文件,结果有限:

DECLARE @res TABLE  
(  
    Value VARCHAR(255)  
    , Data VARCHAR(255)  
);  

INSERT INTO @res  
EXEC master.dbo.xp_instance_regread
         @rootkey      = N'HKEY_LOCAL_MACHINE'
       , @key          = N'Software\Microsoft\MSSQLServer\SQLServerAgent'
       , @value_name   = N'DatabaseMailProfile';

INSERT INTO @res  
EXEC sys.xp_instance_regread
         @rootkey      = N'HKEY_LOCAL_MACHINE'
       , @key          = N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent'
       , @value_name   = N'UseDatabaseMail'

SELECT *
FROM @res;

答案 1 :(得分:0)

这是最终代码。我从scsimon的代码中删除了其他信息。

DECLARE @DatabaseMail VARCHAR(255);  

/*  
    Confirm SQL Server Agent is configured to use Database Mail correctly  
*/  
DECLARE @res TABLE  
(  
    Value VARCHAR(255)  
    , Data VARCHAR(255)  
);  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail';  
INSERT INTO @res  
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile';  
IF (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'UseDatabaseMail' AND r.Data = 1  
    ) = 1 AND   
    (  
        SELECT COUNT(*)  
        FROM @res r  
        WHERE r.Value = 'DatabaseMailProfile' AND r.Data IS NOT NULL  
    ) = 1  
SET @DatabaseMail = 'Configured'  
ELSE  
SET @DatabaseMail = 'Not Configured';  

select @DatabaseMail EnableMailProfile