将内容复制到txt文件中后缀改为vbs
将用户名密码替换为自己的
脚本内容如下:
Option ExplicitOn Error resume next Dim obBaseAppDim objFSODim objTextFileDim strNewAlias,i Dim scrreportDim failedDim addedfailed = 0added = 0 Const ForReading = 1 Set obBaseApp = CreateObject("hMailServer.Application") Call obBaseApp.Authenticate("Administrator","password") '*** N.B. 1. set your administrator password in this line Set objFSO = CreateObject("Scripting.FileSystemObject")Set objTextFile = objFSO.OpenTextFile("users.txt", ForReading) ' *** N.B. 2. Set your CSV path/filename in this lineDo While objTextFile.AtEndOfStream <> TruestrNewAlias = 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 SelectIf err.Number <> 0 Then 'error handling:scrreport = scrreport & "Failed add (probably duplicate) : " & strNewAlias(0) & "," & strNewAlias(1) & "," & strNewAlias(2) & "," & strNewAlias(3) & VBNewLinefailed = failed + 1err.ClearElseadded = added + 1End If i = i + 1Loop scrreport = scrreport & " " & VBNewLinescrreport = scrreport & "Added = " & added & VBNewLinescrreport = scrreport & "Failed or Duplicates = " & failed & VBNewLinewscript.echo scrreport Sub AddAlias(strAlias,strEmailAddress,strDomain)Dim obDomain Dim obAliases Dim obNewAlias Set obDomain = obBaseApp.Domains.ItemByName(strDomain) Set obAliases = obDomain.AliasesSet obNewAlias = obAliases.Add() obNewAlias.Name = strAlias & "@" & strDomain 'usernameobNewAlias.Value = strEmailAddress 'passwordobNewAlias.Active = 1 'activates userobNewAlias.Save() 'saves account Set obNewAlias = NothingSet obAliases = NothingSet obDomain = Nothing End Sub Sub AddUser(strUsername, strPassword, strDomain)Dim obDomain Dim obAccounts Dim obNewAccount Set obDomain = obBaseApp.Domains.ItemByName(strDomain) Set obAccounts = obDomain.AccountsSet obNewAccount = obAccounts.Add() obNewAccount.Address = strUsername & "@" & strDomain 'usernameobNewAccount.Password = strPassword 'passwordobNewAccount.Active = 1 'activates userobNewAccount.Maxsize = 500 'sets mailbox size, 0=unlimitedobNewAccount.Save() 'saves account Set obNewAccount = NothingSet obDomain = Nothing Set obAccounts = Nothing End Sub
users.txt示例
User,tom,password,xxxx.com Alias,jim,tom@xxxx.com,xxxx.com
会在xxxx.com域添加一个用户名为tom 密码为password的用户,并创建将jim@xxxx.com转发至tom@xxxx.com
参考