WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADSI
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Add New Mail Account to users that do not have one

Sometimes ADSI users get into a system without a Exchange email account. This script will add an email account for a user.
AddExtender("wwads34i.dll")

sContext = dsGetProperty("LDAP://RootDSE", "configurationNamingContext")
sContext = "LDAP://%sContext%"
sFilter = "(objectCategory=msExchExchangeServer)"
lPathList = dsFindPath(sContext, sFilter)

count=ItemCount(lPathList,@TAB)
X_Servers=""
For i = 1 To count
   X_Server=ItemExtract(i,lPathList,@TAB)
   X_Server1=ItemExtract(1,X_Server,",")
   X_Server2=ItemExtract(2,X_Server1,"=")
   X_Servers=ItemInsert(X_Server2,i,X_Servers,@TAB)
Next

sAdsiPath="LDAP://rootDSE"
sValue=dsGetProperty(sAdsiPath,"defaultNamingContext")             
sDomainDNS=StrCat("LDAP://",sValue)
domain=dsGetProperty("LDAP://rootDSE","defaultNamingContext")
UsersPath=StrCat("LDAP://cn=users,",domain)

sResults1 = dsFindPath(sDomainDNS,"(&(ObjectCategory=Person)(ObjectClass=user))")

count1=ItemCount(sResults1,@TAB)
users=""
For i = 1 To count1
   user=ItemExtract(i,sResults1,@TAB)
   user1=ItemExtract(1,user,",")
   user2=ItemExtract(2,user1,"=")
   has_mail=dsGetProperty(user,"homeMDB")
   If has_mail=="" Then users=ItemInsert(user2,i,users,@TAB)
Next

xxx=ItemExtract(1,domain,",")
xxx=ItemExtract(2,xxx,"=")
yyy=ItemExtract(2,domain,",")
yyy=ItemExtract(2,yyy,"=")
subdom=xxx
dnsdomain1=StrCat(xxx,".",yyy)
dnsdomain2=StrCat(xxx,".com")

MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`Create User Mailbox`
MyDialogX=015
MyDialogY=098
MyDialogWidth=234
MyDialogHeight=124
MyDialogNumControls=006
MyDialogProcedure=`DEFAULT`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

MyDialog001=`057,093,040,012,PUSHBUTTON,DEFAULT,"Create Mailbox",1,1,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`125,093,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`025,029,084,038,DROPLISTBOX,users,DEFAULT,DEFAULT,3,4,DEFAULT,DEFAULT,DEFAULT`
MyDialog004=`025,045,084,028,DROPLISTBOX,X_Servers,DEFAULT,DEFAULT,6,4,DEFAULT,DEFAULT,DEFAULT`
MyDialog005=`025,023,084,016,STATICTEXT,DEFAULT,"Select User(s) to create Mailboxes",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog006=`113,023,100,020,STATICTEXT,DEFAULT,"This will assume the naming convention of username as mail nickname.",DEFAULT,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog")

count=ItemCount(users,@TAB)
For i = 1 To count
   user=ItemExtract(i,users,@TAB)
   sObjectPath=StrCat("LDAP://CN=",user,",CN=Users,",domain)
   username=dsGetProperty(sObjectPath,"sAMAccountName")
   mail=StrCat(username,"@",dnsdomain1)
   mailnickname=username
   If mail          <> "" Then dsSetProperty(sObjectPath, "userPrincipalName", mail)
   ;e-mail properties:
   homeMDB="CN=Mailbox Store (%X_Servers%),CN=bpk1,CN=InformationStore,CN=%X_Servers%,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=%subdom%,CN=Microsoft Exchange,CN=Services,CN=Configuration,%domain%"
           ;CN=Mailbox Store (SCHLITZ)     ,CN=bpk1,CN=InformationStore,CN=SCHLITZ     ,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=bpk1    ,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=bpk1,DC=com
   homeMTA="CN=Microsoft MTA,CN=%X_Servers%,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=%subdom%,CN=Microsoft Exchange,CN=Services,CN=Configuration,%domain%"
          ;CN=Microsoft MTA,CN=SCHLITZ     ,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=bpk1    ,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=bpk1,DC=com
   LegacyExchangeDN="/o=%subdom%/ou=First Administrative Group/cn=Recipients/cn=%username%"
                    ;/o=bpk1    /ou=First Administrative Group/cn=Recipients/cn=brendan   
   msExchHomeServerName="/o=%subdom%/ou=First Administrative Group/cn=Configuration/cn=Servers/cn=%X_Servers%"
                        ;/o=bpk1    /ou=First Administrative Group/cn=Configuration/cn=Servers/cn=SCHLITZ   
   mdbUseDefaults="1"
   If mail              <> "" Then dsSetProperty(sObjectPath, "mail",mail)
   If mailnickname  <> "" Then dsSetProperty(sObjectPath, "MailNickName", MailNickName)
   If homeMDB           <> "" Then dsSetProperty(sObjectPath, "homemdb", homemdb)
   If homeMTA           <> "" Then dsSetProperty(sObjectPath, "homemta", homemta)
   If legacyExchangeDN  <> "" Then dsSetProperty(sObjectPath, "legacyexchangedn", legacyexchangedn)
   If msExchHomeServerName <> "" Then dsSetProperty(sObjectPath, "msExchHomeServerName", msExchHomeServerName)
   If mdbUseDefaults       <> "" Then dsSetProperty(sObjectPath, "mdbUseDefaults",mdbUseDefaults)
   
   ;can't enable the account until the password is set 
   dsSetProperty(sObjectPath, "UserAccountControl", "512")
   dsSetProperty(sObjectPath, "msExchUserAccountControl","0")
 
   ;now switch to CDO COM object to create the mailbox
   ;Open the  CDO.Person object and go to the account just created
   objperson=ObjectOpen("CDO.Person")
   OPdatasource=objperson.datasource
   opdatasource.open(sObjectPath)
   thisfn=objperson.firstname         ; unnecessary, but it shows you are on the correct user account
   thisln=objperson.lastname
   
   ;don't know where the getinterface method came from or why we need to call it, but it works
   objMailbox=objPerson.GetInterface("IMailboxStore")
   
   ;Use the same homeMDB used when creating the account
   objmailbox.CreateMailbox(homemdb)
   
   ;no need to call the save method, we changed nothing, so just close the object
   ObjectClose(objperson)
Next

Article ID:   W15801
File Created: 2004:03:30:15:40:56
Last Updated: 2004:03:30:15:40:56