2010-08-19 25 views
13

Test sunucumdaki veritabanı posta profilleri & hesaplarını kurmak için SQL Server 2008 GUI'sini kullandım ve şimdi bunları üretim veritabanımıza kopyalamak istiyorum.Veritabanı postasının komut dosyası kurulumu

Bunu yapmak için bir komut dosyası oluşturmanın bir yolu var mı?

cevap

20

AFAIK, bunu zorunlu olarak SSMS'den yazmak için bir yol yoktur, ancak TSQL'de bir kez taşınabilir bir betik oluşturabilir ve tüm sunucularda yeniden kullanabilirsiniz. Burada bununla başlamak için iyi bir örnektir:

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 

Diğer seçenek komut dosyaları oluşturmak için ya .NET veya powershell yoluyla, SMO kaldıraç olacaktır. Bunun için SMO referans olacaktır:

SqlMail Class

GÜNCELLEME:

İşte

o Powershell ve SMO komut olmasını nasıl ortaya çıktı kolaydır:

[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 - Sen kartezyen birleştir/bomba! – MaasSql

+1

Bu müthiş bir keşif! TSQL tarafında, hesabın her sunucunun adına, yani 'donotreply_FooBarSQL @ mycompany.com' 'a özelleştirilmiş olmasını istiyorsanız, belirli parametreler için '@ SERVERNAME' alt girişini kullanabilirsiniz. – NateJ

İlgili konular