While I was setting up my new mail server, I wrote this script to create the users and aliases. You simply put all of the users and the aliases into a CSV file and run the script. Once it finishes, all of your users and aliases should be created for you.
Code:
Option Explicit
Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i
Const ForReading = 1
Set obBaseApp = CreateObject("hMailServer.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("Objects.csv", ForReading) 'Change the name of Objects.csv to the name of your CSV file that you put in the same directory as the script.
Do While objTextFile.AtEndOfStream <> True
strNewAlias = split(objTextFile.Readline, ",")
Select Case strNewAlias(0)
Case "User"
AddUser strNewAlias(1), strNewAlias(2), strNewAlias(3)
Case "Alias"
AddAlias strNewAlias(1), strNewAlias(2), strNewAlias(3)
End Select
i = i + 1
Loop
Sub AddAlias(strAlias,strEmailAddress,strDomain)
Dim obDomain
Dim obAliases
Dim obNewAlias
Set obDomain = obBaseApp.Domains.ItemByName(strDomain)
Set obAliases = obDomain.Aliases
Set obNewAlias = obAliases.Add()
obNewAlias.Name = strAlias & "@" & strDomain 'username
obNewAlias.Value = strEmailAddress 'password
obNewAlias.Active = 1 'activates user
obNewAlias.Save() 'saves account
Set obNewAlias = Nothing
Set obAliases = Nothing
Set obDomain = Nothing
End Sub
Sub AddUser(strUsername, strPassword, strDomain)
Dim obDomain
Dim obAccounts
Dim obNewAccount
Set obDomain = obBaseApp.Domains.ItemByName(strDomain)
Set obAccounts = obDomain.Accounts
Set obNewAccount = obAccounts.Add()
obNewAccount.Address = strUsername & "@" & strDomain 'username
obNewAccount.Password = strPassword 'password
obNewAccount.Active = 1 'activates user
obNewAccount.Maxsize = 0 'sets mailbox size, 0=unlimited
obNewAccount.Save() 'saves account
Set obNewAccount = Nothing
Set obDomain = Nothing
Set obAccounts = Nothing
End Sub
Make sure that the CSV file you create is in the same directory as the sccript otherwise the script will fail. Also, the CSV file needs to be in the following format:
Code:
EntryType,Field1,Field2
The EntryType can be one of two options, User or Alias. If the EntryType is User, then:
Field1 - Username
Field2 - Password
Field3 - DomainName
If the EntryType is Alias, then:
Field1 - AliasName
Field2 - ForwardingEmail
Field3 - DomainName
So, for example, if you had the following CSV file:
Code:
User,tjones,mypassword,jones.com
Alias,tommy,tjones@jones.com,jones.com
This would create a user names
tjones@jones.com in the jones.com domain and an alias
tommy@jones.com which will forward all e-mail to
tjones@jones.com in the jones.com domain.
Hope you find this to be helpful.