Sync user from AD with custom address

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
arthrax
New user
New user
Posts: 13
Joined: 2019-01-10 12:50

Sync user from AD with custom address

Post by arthrax » 2019-02-15 19:33

Hello,

I need to sync in HmailServer user from a specific Active directory OU and set the address from the beginning of Description field.

For exemple : I have an AD account named testrobert and his description fields start with robert@test.com.
Then after sync I get a new account in HMailServer with the Address robert (@test.com is my domain name) and linked to ad account in Active Directory Tab

I have found in this forum this script but I haven't found what to modify : viewtopic.php?f=20&t=21752&p=142392&hil ... ry#p142392

Can someone point me to the right direction ?

Thanks

digim
New user
New user
Posts: 3
Joined: 2019-02-06 22:11

Re: Sync user from AD with custom address

Post by digim » 2019-02-19 10:50

just curious - why you not use native email property?

arthrax
New user
New user
Posts: 13
Joined: 2019-01-10 12:50

Re: Sync user from AD with custom address

Post by arthrax » 2019-02-20 12:00

Hello,

Because we have a directory software which can write on the mail attribute based on check from our mail server.
I'm on discussion with them to find a solution to exclude Hmailserver AD account.

It will be easier to use native email attribute as I have script exemple.

User avatar
mattg
Moderator
Moderator
Posts: 19630
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Sync user from AD with custom address

Post by mattg » 2019-02-21 01:09

arthrax wrote:
2019-02-15 19:33
Can someone point me to the right direction ?
IN the code in the last post in that thread, you probably need to change this line

SamAccountName = ObjMember.samAccountName

But what you change it to will depend on the exact naming convention used in your AD and what that looks like

But I'm not sure if that will work for multiple domains or not
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

arthrax
New user
New user
Posts: 13
Joined: 2019-01-10 12:50

Re: Sync user from AD with custom address

Post by arthrax » 2019-03-12 12:53

Thanks for the direction.

I have finally take some time to work on it and it is working for my case.

The hard part was to check if the account already exist in hMailServer. I use error management to solve it

Here my code for information :

Code: Select all

'On Error Resume Next
' This script import user accounts from active directory
' Change the configuration using the ini file
'
' To run this script, logon or runas a domain user or higher of the domain you wish to import users from
'
Const ADS_UF_ACCOUNTDISABLE = 2 'AD value for account disabled
Dim obApp, obAccount, x, obDomain, bindDomain, shortNameDomain
Set obApp = CreateObject("hMailServer.Application")

'Set here hMailServer login information
Call obApp.Authenticate("Administrator", "password")
bindDomain = "test.com" 'Mail domain in hMailServer
shortNameDomain = "ad.test.com" 'This is the domain name from the drop down when logging in in hMailServer in Active directory tab
'Domain name to add account to
Set obDomain = obApp.Domains.ItemByName(bindDomain)

AddUsers()'Call the function

'msgbox("Completed operation")

Function SaveAccount(ADName,EmailAddress)

On Error Resume Next

Set obAccount1 = obDomain.Accounts.ItemByAddress(EmailAddress) 'Generate an error if account do not exist in hMailServer (Error is : Microsoft VBScript runtime error: Subscript out of range)

if Err.Number <> 0 then 'Case we have an error
	Exist = False
	Err.Clear 'reset error account for next round
else
	Exist = True
end if

if Exist = False then
	'Create a new account
	   Set obAccount = obDomain.Accounts.Add
	   
	   'Set the account properties
	   obAccount.Address =EmailAddress
	   obAccount.Active = True
	   obAccount.MaxSize = 5 'Max mailbox size in MB
	   obAccount.IsAD = True
	   obAccount.ADUsername = ADName
	   obAccount.ADDomain = shortNameDomain
	   'msgbox("Account " & ADName & " was saved")
	   obAccount.Save

   else
	   'msgbox("Account " & ADName & " already exist")
   End If
End Function

Function AddUsers()
    'Bind to connected domain
    Set objRoot = GetObject("LDAP://RootDSE")
    strDNC = objRoot.Get("DefaultNamingContext")
	strOU = "OU=Messaging,OU=Applications,OU=_GLB" 'Root OU for user
	Set objDomain = GetObject("LDAP://" & strOU & "," & strDNC)
    Call FindUsers(objDomain)
End Function

Sub FindUsers(objDomain)
    'On Error Resume Next

    For Each objMember In objDomain
		If ObjMember.Class = "user" Then
			SamAccountName = ObjMember.samAccountName
			Description = ObjMember.description
			'AD Descritption attribute contains email address then a space and other information
			if Description <> "" then
				SpaceNumber = Instr(Description, " " ) 'Get the number in the text string where we have the first space
				Email = Left (Description, SpaceNumber -1)	'Get the text from the left to the first space
				intUAC=ObjMember.userAccountControl 'Check if AD account is enable or not
				If intUAC AND ADS_UF_ACCOUNTDISABLE Then 
					msgbox(ObjMember.distinguishedName & " is disabled")
				else
					Call SaveAccount(SamAccountName,Email)'Call the function with 2 arguments
				End If
				SamAccountName = "-"
			end If
		End If
      
      'Recusively search through OU structure for users
      If objMember.Class = "organizationalUnit" or OBjMember.Class = "container" Then
          FindUsers(objMember)
      End If
    Next
End Sub 

Post Reply