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

Nathan Sain natesain@yahoo.com
Wed, 1 May 2002 14:56:43 -0700 (PDT)


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