2010-08-19 5 views
13

Ich habe die SQL Server 2008 GUI verwendet, um Datenbank-Mail-Profile & Konten auf meinem Testserver einzurichten, und ich möchte jetzt diese in unserer Produktionsdatenbank duplizieren.Scripting Setup der Datenbank Mail

Gibt es eine Möglichkeit, ein Skript zu generieren, um dies zu tun?

Antwort

20

AFAIK, es gibt keine Möglichkeit, dies von SSMS aus zu skripten, aber Sie können ein transportierbares Skript einmal in TSQL erstellen und es auf allen Servern wiederverwenden. Hier ist ein gutes Beispiel, um Sie mit diesem begonnen:

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 protected]', -- 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 

Die andere Option wäre SMO zu nutzen, entweder durch .NET oder Powershell die Skripte zu generieren. Die SMO Referenz hierfür wäre:

SqlMail Class

UPDATE:

Hier ist, wie einfach es stellte sich dies Skript heraus mit Powershell und 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(); 
+1

Jonathan - Sie die kartesische Verbindung/Bombe! – MaasSql

+1

Das war ein toller Fund! Auf der TSQL-Seite können Sie '@@ SERVERNAME' verwenden, um bestimmte Parameter zu sub-in, wenn Sie möchten, dass das Konto an den Namen jedes Servers angepasst wird, d. H.' Donotreply_FooBarSQL @ mycompany.com'. – NateJ