944,114 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 7377
  • Python RSS
Jul 26th, 2007
0

Adding user and mailbox in Active Directory

Expand Post »
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:
python Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drexlus is offline Offline
3 posts
since Jul 2007
Jul 27th, 2007
0

Re: Adding user and mailbox in Active Directory

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jul 30th, 2007
0

Re: Adding user and mailbox in Active Directory

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drexlus is offline Offline
3 posts
since Jul 2007
Aug 2nd, 2007
0

Re: Adding user and mailbox in Active Directory

Alright, so I have gotten a bit further, but could use a bit more help. My code now looks like:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drexlus is offline Offline
3 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Adding user and mailbox in Active Directory

Hi,
Quote ...
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

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 34
Posting Whiz in Training
katharnakh is offline Offline
237 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Expanded Calendar utilizing wxPython
Next Thread in Python Forum Timeline: need help creating simple program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC