Assigning an smtp email address to an OU

I would like to assign/attach an email address to an OU that would act similiar to a distribution group. Simply put, I have OUs for each of my regions accross the world. When I add a new user object into one of these regional OUs I would like for it to automatically be receiving emails sent to that office.

I realize I could just create a distro group for that OU, but I am trying to automate as much as possible.

If it helps, I am using Active Directory with Exchange 2003.
Who is Participating?
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

Here we go.

I'm sure we'll need to play around with it a fair bit before it'll do everything you need.

First things first, what the script does...

You have to configure ROOT_OU, the NETBIOS_DOMAIN_NAME and add the groups you want to use (along with the OU Name) to objOUsToGroups (as in the examples below).

Then the script will connect to ROOT_OU, look for every sub-OU, then for each sub-OU it'll add the user into the respective group (provided it has an entry in objOUsToGroups).

Does that make any kind of sense? :)

' VbScript to add users to a group based on their Organisational Unit
' Information that must be set:
Const ROOT_OU = "OU=Root,DC=Domain,DC=local"
Const NETBIOS_DOMAIN_NAME = "YourDomain"
' Unless the names of the Groups are going to match the OUs we need to hard-code this lot:
Dim objOUsToGroups : Set objOUsToGroups = CreateObject("Scripting.Dictionary")
objOUsToGroups.Add "OU 1 Name", "Group 1 Name"
objOUsToGroups.Add "OU 2 Name", "Group 2 Name"
' Functions
Function GetObjectDN(strObject, strDomain)
	' Return Type: String
	' Returns a Distinguished Name for an Object from it's NT SAM ID.
	' This will only function for valid object types within an NT Domain structure.
	Const ADS_NAME_TYPE_1779 = 1
	Const ADS_NAME_TYPE_NT4 = 3
	Dim objNameTranslate
	Dim strObjectDN
	On Error Resume Next : Err.Clear
	Set objNameTranslate = CreateObject("NameTranslate")
	objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
	objNameTranslate.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strObject
	strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
	If Err.Number <> 0 Then  ' Make the DN Blank for a Failed Search
		strObjectDN = ""
	End If
	Set objNameTranslate = Nothing
	On Error Goto 0
	GetObjectDN = strObjectDN
End Function
' Main Code
Dim objRoot, objOU, objUser, objGroup
Dim strDN
' Unless the names of the Groups are going to match the OUs we need to hard-code this lot:
' Then connect to the Root_OU
Set objRoot = GetObject("LDAP://" & ROOT_OU)
' Connect to the Root OU and loop through the sub-OUs
For Each objOU in objRoot
	If objOU.Class = "organizationalUnit" Then
		' If we have an entry in OUsToGroups we can add the users in that OU to the group
		If objOUsToGroups.Exists(objOU.Get("name")) Then
			' Try and get the DistinguishedName for the Group so we don't have to 
			' hard-code them all.
			strDN = GetObjectDN(objOU.Get("name"), NETBIOS_DOMAIN_NAME)
			If strDN <> "" Then
				' Connect to the Group
				Set objGroup = GetObject("LDAP://" & strDN)
				' Add each User to the Group
				For Each objUser in objOU
					objGroup.Add objUser.ADSPath
				Set objGroup = Nothing
			End If
		End If
	End If
Set objRoot = Nothing
Set objOUsToGroups = Nothing

Open in new window

Chris DentPowerShell DeveloperCommented:

You can't assign an address to an OU.

But, you could use a Query-Based Distribution Group with the base of the filter set to that OU. That would keep it nice and up to date.

Otherwise you'd have to script modification of the membership of a standard group on a regular basis, very easy to do if you don't want to play with the Query-Based lists.



CogentcoAuthor Commented:
Ok, the QBDG is out as we still have one old Exchange 5.5 server on our network (need to be running in native mode for the queries to work). I think that is exactly waht I was looking for too... bummer.

Any ideas as to where I could find a precanned script to accomlish this? My scripting skills are limited to modifying existing ones, not creating them.

Thanks for the input!
Chris DentPowerShell DeveloperCommented:

I'll help you do one of those, dead easy to write those for AD :)

Won't be a moment.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.