Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…

17Dec/170

Powershell: Manage IIS SMTP server

Setting up Microsoft Internet Information Services (IIS) SMTP service is pretty straight forward for simple implementations. It hasn't really keept up with time and I'm pretty sure not to many people use it anymore. Working with an older implementation in a system that used distributed SMTP on each and every IIS server I realized we needed to centralize it so we could secure it properly. This included re configuring an old IIS SMTP server and then add a bunch of aliases to make sure the server accepted all the incoming e-mails.

Configuring IIS SMTP

To reconfigure the SMTP we can use Active Directory Service Interface from Powershell. This allows us to change settings we can't from the GUI like pickup and badmail directories. Here is a simple code snippet how to handle configuration:

$SMTPServer =[adsi]("IIS://localhost/SMTPSVC/1")
$SMTPServer.BadMailDirectory = "D:\mailroot\Badmail"
$SMTPServer.DropDirectory = "D:\mailroot\Drop"
$SMTPServer.PickupDirectory = "D:\mailroot\Pickup"
$SMTPServer.QueueDirectory = "D:\mailroot\Queue"
$SMTPServer.CommitChanges()

Adding SMTP Aliases

By adding domain aliases you will force the SMTP service to accept e-mails for additional domains. From what I found online there was a lot of Powershell snippets for adding complete new domains but nothing handling the aliases. Here is the code:

function AddSMTPAlias {
Param([Parameter(Mandatory=$true)]$DomainName,[Parameter(Mandatory=$true)]$SMTPvsNbr)

$SMTPDomain =[adsi]("IIS://localhost/SMTPSVC/" + $SMTPvsNbr + "/Domain")
$NewEntry = New-Object System.DirectoryServices.DirectoryEntry($SMTPDomain.Path)
$NewEntryCollection = $NewEntry.Children
$NewEntryToAdd = $NewEntryCollection.Add($DomainName, $NewEntry.SchemaClassName)
$NewEntryToAdd.RouteAction = 16
$NewEntryToAdd.CommitChanges()
}

$domains = Get-Content 'c:\temp\domains.txt'

foreach($domain in $domains)
{
AddSMTPAlias -DomainName $domain -SMTPvsNbr 1
}

The function takes two parameters, first up is the DomainName which is pretty self explanatory. The second one is SMTPvsNbr which is the number of the SMTP Service in the IIS. You can find this in the GUI and when you list all the SMTP Services via Powershell. The secrete source here is the RouteAction equals 16 which means domain alias.

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

No trackbacks yet.