I have used the win32net, win32netcon, win32api modules to create users.  Function 
below does the job - where the input dictionary (dict) is of the form:

 d = {'Login':'user',
                  'FullName':'abc def',
                  'Desc':'some descriptioni',
                  'Password':'top secret',
                  'Groups': ('Users', 'FancyClub'),
                  'Home': 'directory path'},


def CreateUser(dict):
    # Check if user already exist --> skip
    try:
        d = win32net.NetUserGetInfo(None, dict['Login'], 1)
        return
    except:
        pass

    d = {}
    d['name'] = str(dict['Login'])
    d['password'] = str(dict['Password'])
    d['comment'] = str(dict['Desc'])
    d['flags'] = win32netcon.UF_NORMAL_ACCOUNT | win32netcon.UF_SCRIPT | 
win32netcon.UF_PASSWD_CANT_CHANGE | win32netcon.UF_DONT_EXPIRE_PASSWD
    d['priv'] = win32netcon.USER_PRIV_USER
    d['home_dir'] = str(dict['Home'])
    win32net.NetUserAdd(None, 1, d)
    
    #d = win32net.NetUserGetInfo(None, 'TestUser', 10)
    #d['full_name'] = dict['FullName']
    #d = win32net.NetUserSetInfo(None, 'TestUser', 10, d)

    domain = win32api.GetDomainName()
    d = [{"domainandname" : domain+"\\"+dict['Login']}]
    for gr in dict['Groups']:
        win32net.NetLocalGroupAddMembers(None, gr, 3, d)


As you can see, I have used the 'str' function.  I think I have some problems with 
string/UNICODE, because I got error message if I did not do this.  Also, note that the 
3 lines of code to set the full name (which is not part of the USER_INFO_1 struct - 
see Mark Hammond / Andy Robinson book "Python Programming on Win32") have been 
commented out.  These also produce the same error, have not been able to figure out 
what it is.  According to the mentioned book - this should work.  Maybe someone knows 
what is wrong.

Anyhow - hope this helps.

Nikolai Kirsebom



-----Original Message-----
From:   Noah [SMTP:[EMAIL PROTECTED]]
Sent:   Thursday, October 25, 2001 4:35 AM
To:     [EMAIL PROTECTED]
Subject:        Creating a user with Python

Does anyone have sample code to create a user
on a local or a remote NT machine?

I'm trying to sort through this WMI stuff
because it seems like that would allow me to
create a user on a local or a remote machine,
but it's taking forever to figure out where
to start.

Anyone have experience with WMI and Python?

Yours,
Noah

_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython


_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython


Reply via email to