• mrrobsewell (6/24/2013)


    I love Powershell

    [void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");

    $Server = "ServerName";

    $srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server $Server

    $srv.Mail.Script();

    That's interesting; i bet with some enhancements, that could do the same thing my script was;

    i see quite a few differences, so with a 1:1 comparison, my little script is still better, but i'd have to wrestle with powershell and see if I can coax all the same settings out that my script does;

    i bet the If Not Exists() is just a setting in SMO.

    The scripting of an account in powershell needs a lot of work;

    EXEC msdb.dbo.sysmail_add_account_sp

    @account_name=N'sqlnotifications@gmail.com',

    @email_address=N'sqlnotifications@gmail.com',

    @display_name=N'SQL Notifications',

    @replyto_address=N'sqlnotifications@gmail.com',

    @description=N'Google gMail account'

    but my script gets quite a few more settings:

    IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_account WHERE name = 'sqlnotifications@gmail.com')

    BEGIN

    --CREATE Account [sqlnotifications@gmail.com]

    EXECUTE msdb.dbo.sysmail_add_account_sp

    @account_name = 'sqlnotifications@gmail.com',

    @email_address = 'sqlnotifications@gmail.com',

    @display_name = 'SQL Notifications',

    @replyto_address = 'sqlnotifications@gmail.com',

    @description = 'Google gMail account',

    @mailserver_name = 'smtp.googlemail.com',

    @mailserver_type = 'SMTP',

    @port = '587',

    @username = 'sqlnotifications@gmail.com',

    @password = 'NotTheRealPassword',

    @use_default_credentials = 0 ,

    @enable_ssl = 0 ;

    END --IF EXISTS account

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!