Adding user and mailbox in Active Directory

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 3
Reputation: drexlus is an unknown quantity at this point 
Solved Threads: 0
drexlus drexlus is offline Offline
Newbie Poster

Adding user and mailbox in Active Directory

 
0
  #1
Jul 26th, 2007
I am new to Python and, in fact, I have limited programming experience in general.

I am attempting to create a web form that our HR employees can use to add new employees into Active Directory and to create a mailbox for that user. AD is on a WIN2k server and Exchange 2000. I have the form and some code that will add the user based on submitted fields from the webform. The code fails when it gets to the part about creating the mailbox. Below is my code:
  1. import win32com.client
  2. import win32com, win32com.adsi
  3. import pythoncom
  4. import cgi
  5.  
  6. FormFile = "addusertmp.html"
  7. print 'Content-Type: text/html\n\n'
  8. def DisplayForm():
  9. FormHandle = open(FormFile, "r")
  10. FormInput = FormHandle.read()
  11. FormHandle.close()
  12. print FormInput
  13.  
  14. def ProcessForm(entries):
  15. first = entries['first'].value
  16. last = entries['last'].value
  17. login = entries['login'].value
  18. password = entries['password'].value
  19. title = entries['title'].value
  20.  
  21. entries = cgi.FieldStorage()
  22.  
  23. def add_acct (location, user):
  24. ad_obj=win32com.client.GetObject(location)
  25.  
  26. ad_user=ad_obj.Create('user','cn='+user['login'])
  27. ad_user.Put('sAMAccountName',user['login'])
  28. ad_user.Put('userPrincipalName',user['login']+'@domain.org')
  29. ad_user.Put('DisplayName',user['first']+' '+user['last'])
  30. ad_user.Put('givenName',user['first'])
  31. ad_user.Put('sn',user['last'])
  32. ad_user.Put('description',user['title'])
  33. ad_user.SetInfo();ad_user.GetInfo()
  34. ad_user.AccountDisabled=0
  35. ad_user.setpassword(user['password'])
  36. ad_user.SetInfo()
  37. ad_user.CreateMailbox('CN=Maibox Store (SERVER),CN=First Storage Group,CN=InformationStore,CN=SERVER,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=Company Name,CN=Microsoft Exchange,CN=Services,CN=Configuration'+location)
  38. ad_user.EmailAddress=user+'@domain.org'
  39. ad_user.SetInfo()
  40. ad_user.Put('msExchUserAccountControl',2)
  41. ad_user.SetInfo()
  42.  
  43. if entries:
  44. location='LDAP://OU=Employees,DC=domain,DC=org'
  45. user={'first':entries.getvalue('first'),'last':entries.getvalue('last'),'login':entries.getvalue('login'),'title':entries.getvalue('title'),'password':entries.getvalue('password')}
  46. print user
  47. add_acct(location,user)
  48. print "user added"
  49. else:
  50. DisplayForm()

When I go to the web form and I enter&submit the required fields, this creates the user & enables the user, but also produces the following error and never creates the mailbox:
{'password': 'test', 'login': 'bbonez', 'title': 'Bouncer', 'last': 'Bonez', 'first': 'Big'} Traceback (most recent call last): File "W:\SysadminSite\adduser2.cgp", line 50, in ? add_acct(location,user) File "W:\SysadminSite\adduser2.cgp", line 37, in add_acct ad_user.CreateMailbox('CN=Maibox Store (SERVER),CN=First Storage Group,CN=InformationStore,CN=SERVER,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=Company Name,CN=Microsoft Exchange,CN=Services,CN=Configuration'+location) File "C:\Python22\Lib\site-packages\win32com\client\dynamic.py", line 438, in __getattr__ raise AttributeError, "%s.%s" % (self._username_, attr) AttributeError: .CreateMailbox

Any help would be GREATLY appreciated. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Adding user and mailbox in Active Directory

 
0
  #2
Jul 27th, 2007
It looks like your problem is on the Windows end rather than the Python end. Apparently, a 'user' object's CreateMailbox() method requires something different from what you are supplying. As a result, Server 2k is choking but (as usual) isn't telling you exactly why.

If the error were Python-related, you would get a much more descriptive message .

We did something like what you are trying to do at school, but handled it by having Python create a .csv file for all users and then used Active Directory to import the users from the .csv file. I can't recall the details at the moment, but it seems unlikely to be appropriate to your situation.

I would recommend the following:

* Try commenting out the code that concerns mailbox creation (lines 37-41, if I'm reading it right).
* Make sure that you can use the web form to create a new user. Make sure the new user goes into the right OU and is a member of all of the right security groups!
* Check your Server 2k documentation for the right syntax for the CreateMailbox. See if you can debug lines 37-41 that way.

Hope it helps,
Jeff
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 3
Reputation: drexlus is an unknown quantity at this point 
Solved Threads: 0
drexlus drexlus is offline Offline
Newbie Poster

Re: Adding user and mailbox in Active Directory

 
0
  #3
Jul 30th, 2007
Thanks for the insight. The code does, in fact, create the user in the proper groups. It creates it with the correct password and the account is enabled.

So it makes sense what you're saying about Server 2k rejecting the "CreateMailbox" syntax. I will see if I can research what I need to be using to get that mailbox created. If anybody can help me out here, I would appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 3
Reputation: drexlus is an unknown quantity at this point 
Solved Threads: 0
drexlus drexlus is offline Offline
Newbie Poster

Re: Adding user and mailbox in Active Directory

 
0
  #4
Aug 2nd, 2007
Alright, so I have gotten a bit further, but could use a bit more help. My code now looks like:
  1. import win32com.client
  2. import win32com, win32com.adsi
  3. import cdoexm
  4. import pythoncom
  5. import cgi
  6.  
  7. FormFile = "addusertmp.html"
  8. print 'Content-Type: text/html\n\n'
  9. def DisplayForm():
  10. FormHandle = open(FormFile, "r")
  11. FormInput = FormHandle.read()
  12. FormHandle.close()
  13. print FormInput
  14.  
  15. def ProcessForm(entries):
  16. first = entries['first'].value
  17. last = entries['last'].value
  18. login = entries['login'].value
  19. password = entries['password'].value
  20. title = entries['title'].value
  21.  
  22. entries = cgi.FieldStorage()
  23.  
  24. def add_acct (location, user):
  25. ad_obj=win32com.client.GetObject(location)
  26.  
  27. ad_user=ad_obj.Create('user','cn='+user['login'])
  28. ad_user.Put('sAMAccountName',user['login'])
  29. ad_user.Put('userPrincipalName',user['login']+'@garcoschools.org')
  30. ad_user.Put('DisplayName',user['first']+' '+user['last'])
  31. ad_user.Put('givenName',user['first'])
  32. ad_user.Put('sn',user['last'])
  33. ad_user.Put('description',user['title'])
  34. ad_user.SetInfo();ad_user.GetInfo()
  35. ad_user.AccountDisabled=0
  36. ad_user.setpassword(user['password'])
  37. ad_user.SetInfo()
  38. ad_user.MailEnable("SMTP:"+user['login']+"@domain.org")
  39. ad_user.SetInfo()
  40. HomeMDB=('CN=Mailbox Store (SERVER),CN=First Storage Group,CN=InformationStore,CN=SERVER,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=Company name,CN=Microsoft Exchange,CN=Services,CN=Configuration,OU=Employees,DC=domain,DC=org')
  41. cdoexm.IMailboxStore.CreateMailbox(HomeMDB)
  42. ad_user.EmailAddress=user['login']+'@domain.org'
  43. ad_user.SetInfo()
  44. ad_user.Put('msExchUserAccountControl',2)
  45. ad_user.SetInfo()
  46.  
  47.  
  48. entries=1 #test line, remove later
  49. if entries:
  50. location='LDAP://OU=Employees,DC=domain,DC=org'
  51. # user={'first':entries.getvalue('first'),'last':entries.getvalue('last'),'login':entries.getvalue('login'),'title':entries.getvalue('title'),'password':entries.getvalue('password')}
  52. user={'first':'jason','last':'bourne','login':'bbourne2','title':'Assassin','password':'password'} #testline, remove later
  53. print user
  54. add_acct(location,user)
  55. print "user added"
  56. else:
  57. DisplayForm()

The error I get now is:
File "E:\Projects\Python\SysAdmin\tests\adduser2.py", line 41, in add_acct
cdoexm.IMailboxStore.CreateMailbox(HomeMDB)
TypeError: unbound method CreateMailbox() must be called with IMailboxStore instance as first argument (got str instance instead)

Any idea what I'm doing wrong? Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Adding user and mailbox in Active Directory

 
0
  #5
Aug 3rd, 2007
Hi,
The error I get now is:
File "E:\Projects\Python\SysAdmin\tests\adduser2.py", line 41, in add_acct
cdoexm.IMailboxStore.CreateMailbox(HomeMDB)
TypeError: unbound method CreateMailbox() must be called with IMailboxStore instance as first argument (got str instance instead)
this is what it comes to me at the moment, CreateMailBox() method of IMailboxStore class.

So when you are calling a member function class, you can call member function in two ways..
- using instance of the class and function parameters
- using class name, and passing the instance of the class as first argument and function parameters following it, to the function.

In your case you are passing an argument of string type as first argument, hence you get TyoeError exception. So you need to pass, an instance of IMailboxStore class as first argument.

Below is the sample code, which is similar to your's.. helps you in better understanding the problem

  1. >>> class test:
  2. def fun(self):
  3. print 'hi'
  4.  
  5.  
  6. >>> t=test() # creating an instance
  7. >>> t.fun() # calling function using instance of the class itself
  8. hi
  9. >>> test.fun()
  10.  
  11. Traceback (most recent call last):
  12. File "<pyshell#11>", line 1, in -toplevel-
  13. test.fun()
  14. TypeError: unbound method fun() must be called with test instance as first argument (got nothing instead)
  15. >>> test.fun(t) # calling member function with the instance of the class
  16. hi
  17. >>>

Hope i explained the problem properly, all the best.

kath.
Last edited by katharnakh; Aug 3rd, 2007 at 3:21 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC