Win32 question: Problem with NetLocalGroupDelMembers

Mark Hammond mhammond at
Fri Sep 6 23:10:57 EDT 2002

Marten Hedman wrote:
> Hi,
> Im writing a script for administrating local user groups on a remote 
> WinNT computer, using Python 2.2.1 and the Win32 extensions, build 148. 
> Getting information about users in a group and adding users to a group 
> works fine, but when I try to delete users from a group I get an 
> exception, 'TypeError: The object can not be converted to a Unicode 
> object'. An interactive example session looks like this:

This is a bug in the documentation.

>  >>> win32net.NetLocalGroupDelMembers(server, group, mem)
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: The object can not be converted to a Unicode object

This function expects a list of string usernames, *not* 
PyLOCALGROUP_INFO structures.  Thus, the following will work:

names = [ m['domainandname'] for m in mem ]
win32net.NetLocalGroupDelMembers(server, group, names)

I added the following function to


def LocalGroup(uname=None):
     "Creates a local group, adds some members, deletes them, then 
removes the group"
     level = 3
     if uname is None: uname=win32api.GetUserName()
     if uname.find("\\")<0:
         uname = win32api.GetDomainName() + "\\" + uname
     group = 'python_test_group'
     # delete the group if it already exists
         win32net.NetLocalGroupDel(server, group)
         print "WARNING: existing local group '%s' has been deleted."
     except win32net.error:
     group_data = {'name': group}
     win32net.NetLocalGroupAdd(server, 1, group_data)
         u={'domainandname': uname}
         win32net.NetLocalGroupAddMembers(server, group, level, [u])
         mem, tot, res = win32net.NetLocalGroupGetMembers(server, group, 
         print "members are", mem
         if mem[0]['domainandname'] != uname:
             print "ERROR: LocalGroup just added %s, but members are %r" 
% (uname, mem)
         # Convert the list of dicts to a list of strings.
         win32net.NetLocalGroupDelMembers(server, group, 
[m['domainandname'] for m in mem])
         win32net.NetLocalGroupDel(server, group)
     print "Created a local group, added and removed members, then 
deleted the group"

More information about the Python-list mailing list