Active directory is wordy and very detailed. It can be daunting to first figure out what you need to know to interface with it. Here is some simple code to discover information about the exchange environment you are in. This isn't a full discussion of how to manage exchange with active directory. I thought I'd get something out now and then plan to add that when I get time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import win32com,win32com.client,pythoncom
import time,os,string
def code_tup(l=''):
return string.join(l,"','")
#get base LDAP string
ldap_loc=win32com.client.GetObject('LDAP://rootDSE').Get("defaultNamingContext")
print 'Found the following:'
print '\tbase ldap string: ',ldap_loc
#get exchange site
ldap_ex='CN=Microsoft Exchange,CN=Services,CN=Configuration'
ex_sites=[]
msg=''
try:
for i in win32com.client.GetObject('LDAP://'+ldap_ex+','+ldap_loc):
if i.cn!='Active Directory Connections': ex_sites.append(i.cn)
except pythoncom.com_error,(hr,msg,exc,arg):
pass
if msg:
print 'Failed on first attempt contacting exchange in Active Directory at\n',ldap_loc,'\n',msg
ldap_loc=string.join(ldap_loc.split(',')[1:],',')
print 'Now trying',ldap_loc
try:
for i in win32com.client.GetObject('LDAP://'+ldap_ex+','+ldap_loc):
if i.cn!='Active Directory Connections': ex_sites.append(i.cn)
except pythoncom.com_error,(hr,msg,exc,arg):
print msg
print 'Cannot find exchange',sys.exit(1)
print '\tSites are:',string.join(ex_sites)
ex_server=[]
for ex_site in ex_sites:
print 'At',ex_site
####get the exchange servers
ex_admin_grps='CN=Administrative Groups,cn='+ex_site+','+ldap_ex+','+ldap_loc
try:
admin_grp=win32com.client.GetObject('LDAP://'+ex_admin_grps)[0].cn
except pythoncom.com_error,(hr,msg,exc,arg):
print 'Cannot find an Administrative Group',msg,'\nAt ',ex_admin_grps
continue
print ' Administrative Group:',admin_grp
ldap_ex_srv='CN=Servers,'+'cn='+admin_grp+','+ex_admin_grps
ex_servers=[]
for server in win32com.client.GetObject('LDAP://'+ldap_ex_srv):
ex_servers.append(server.cn)
print ' Exchange servers:',string.join(ex_servers)
####get the information stores
ldap_info_store='CN=InformationStore,CN=%s,CN=Servers,CN=%s,%s'%(ex_servers[-1],admin_grp,ex_admin_grps)
ex_stores=[]
for info_store in win32com.client.GetObject('LDAP://'+ldap_info_store):
print ' At Information store:',info_store.cn
ldap_store='CN='+info_store.cn+','+ldap_info_store
for store in win32com.client.GetObject('LDAP://'+ldap_store):
print ' Store:',store.cn
ex_stores.append('cn='+store.cn+','+ldap_store)
#save it to a file:
config_file='Ad_config_'+ex_site.lower()+'.py'
if os.path.exists(config_file):
os.rename(config_file,config_file+'_'+str(int(time.time()))+'.txt')
f=open(config_file,'w')
f.write("ldap_loc='%s'\n"%(ldap_loc))
f.write("ex_site='%s'\n"%(ex_sites[0]))
f.write("ex_servers=('%s')\n\n"%(code_tup(ex_servers)))
f.write("ex_stores=('%s')\n\n"%(code_tup(ex_stores)))
#find mailbox store:
found=0
ex_mail_stores=[]
for i in ex_stores:
if i.find('Mailbox Store')!=-1: ex_mail_stores.append(i)
found=1
if not(found):
f.write("ex_mail_stores='???%s\n\n'\n\n"%(ex_stores[0]))
else:
f.write("ex_mail_store=('%s')"%(code_tup(ex_mail_stores)))
f.close()
|
This program will try to give you the basic information for an exchange site that you can use to manage exchange. It simply builds ldap strings upon strings. It also writes out to a ".py" file data that you can use to import.
I'll add more information about how to use this information in another document.
Works well once variable name is fixed... I had to change line 66 to read:
for store in win32com.client.GetObject('LDAP://'+ldap_store):
instead of:
for store in win32com.client.GetObject('LDAP://'+ldap_ex_store):
to use the variable ldap_store set on line 64. Once I did this, the script worked fine. Just a typo.
Thanks. Thanks for noticing the typo. I fixed it.
for multiple admin groups. the script breaks if you have more than one admin group, like we do. A simple for loop and one or two changes sorts that out:
for admin_grp in admin_grps: