ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Discover exchange in active directory
Submitter: John Nielsen (other recipes)
Last Updated: 2004/09/04
Version no: 1.1
Category: System

 

Not Rated yet


Description:

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.

Source: Text Source

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()

Discussion:

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.



Add comment

Number of comments: 3

Works well once variable name is fixed..., Brian Jarrett, 2003/03/12
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.
Add comment

Thanks., John Nielsen, 2004/09/04
Thanks for noticing the typo. I fixed it.
Add comment

for multiple admin groups, Warren Ali, 2005/01/11
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:
print ' Administrative Group:',admin_grp.cn
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Treat the Win32 Registry ...

5. a friendly mkdir()

6. Wrapping template engine ...

7. Assignment in expression

8. Changing return value ...

9. Implementation of sets ...

10. bag collection class




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.