Configuring Database Mail with Powershell and SMO

  • Comments posted to this topic are about the item Configuring Database Mail with Powershell and SMO

  • Great Bruce

    Another wizard I will never open again because I now have the perfect script to replace it!

    I just added [cmdletbinding()] and the server name as a param.

    Now working on a script to create operators.

    thank you very much.

    Klaas

  • Glad you liked it. Here's what I use for operators.

    # Configure the SQL Agent to use dbMail and add operators .

    $sqlSrvObject.JobServer.AgentMailType = 'DatabaseMail'

    $sqlSrvObject.JobServer.DatabaseMailProfile = $yourProfileNameHere

    $o = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.Operator($sqlSrvObject.JobServer, 'YourOperatorNameYere')

    $o.EmailAddress = 'mailTo@yourDomain.tld'

    $o.Create()

    # or

    $o = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.Operator($sqlSrvObject.JobServer, $operatorNameHere)

    $o.EmailAddress = $yourMailToAddressHere

    $o.Create()

    $sqlSrvObject.JobServer.Alter()

    I haven't parameterized either since I don't have to set up mail or add operators too often, usually just when doing a new install.

  • That 's much easier than I expected.

    I found a T-sql script, but I prefer the Powershell way.

    I added those 4 lines as Step 10 in the Database Mail setup script, since I always need the 'helpdesk' operator whenever I install database mail.

    Thanks again!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply