Domanda

Ho usato la GUI di SQL Server 2008 per configurare database di profili e account di posta elettronica sul mio server di prova, e mi piacerebbe ora come duplicare quelle al nostro database di produzione.

C'è un modo per generare uno script per fare questo?

È stato utile?

Soluzione

Per quanto ne sappia, non c'è un modo per necessariamente questo script da SSMS ma è possibile creare uno script trasportabile in TSQL una volta e riutilizzarlo su tutti i server. Ecco un buon esempio per iniziare con questo:

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

L'altra opzione sarebbe quella di sfruttare SMO, sia attraverso .NET o PowerShell per generare gli script. Il riferimento SMO per questo sarebbe:

SqlMail Classe

UPDATE:

Ecco come facile si è rivelato essere quello di questo script con PowerShell e 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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top