[python-win32] User admin with ADSI / LDAP and python

Jens B. Jorgensen jens.jorgensen@tallan.com
Thu, 02 May 2002 12:30:56 -0500


I've used the LDAP namespace of ADSI but not for hitting an actual 
Active Directory, but rather against an exchange server and for 
manipulating local users. I'm going to be though that if the below is 
actual code then your problem is that

    newUser.put("samccountname",logonName)

should be:

    newUser.put("samaccountname",logonName)


Nathan Sain wrote:

>I've been working on creating a simple script to
>manage users in different parts of a directory on a
>Win2000 AD server.  From looking around the following
>code seems to be correct, but always results in an
>error.  If anyone that has any experience working with
>AD via ADSI and python would please review the
>following code and make suggestions I would appreciate
>it!  
>
>the code is as follows:
>
>import win32com.client
>import sys
>
>def getDSO(dirPath,dirUser,password):
>    adsi = win32com.client.Dispatch('ADsNameSpaces')
>    ldap = adsi.getobject('','LDAP:')
>    DSO =
>ldap.OpenDSObject(dirPath,dirUser,password,0)
>    print DSO.ADsPath
>    return DSO
>
>def makeUser(DSO,fullName,logonName,**kwargs):
>    newUser = DSO.create("User","cn=%s" % fullName)
>    print newUser.Class
>    print newUser.ADsPath
>    newUser.put("samccountname",logonName)
>    newUser.put("displayname",fullName)
>    newUser.Put("useraccountcontrol",1)
>    newUser.SetInfo()
>    return 1
>
>if __name__ == '__main__':
>    logon_name =
>'cn=Administrator,cn=Users,dc=school,dc=us'
>    password = 'password'
>    student_ou =
>'LDAP://server/ou=Students,dc=school,dc=us'
>    DSO = getDSO(student_ou,logon_name,password)
>    print makeUser(DSO,'Joe User','juser')
>
>results in the error:
>
>  File "C:\usertools\setupUser.py", line 28, in
>makeUser
>    newUser.SetInfo()
>  File "<COMObject <unknown>>", line 2, in SetInfo
>com_error: (-2147352567, 'Exception occurred.', (0,
>None, None, None, 0, -2147016694), None)
>
>__________________________________________________
>Do You Yahoo!?
>Yahoo! Health - your guide to health and wellness
>http://health.yahoo.com
>
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32@python.org
>http://mail.python.org/mailman/listinfo/python-win32
>
>  
>


-- 
Jens B. Jorgensen
jens.jorgensen@tallan.com