Add a user to a local group


This scripts shows how to add a user to a local group on a machine.


VBScript Example


function add_to_local_group( machine, account, local_group_name )
set object_to_add = GetObject(“WinNT://” & account )
set local_group = GetObject(“WinNT://” & machine & “/” + local_group_name & “,group”)
local_group.Add( object_to_add.AdsPath )
end function

this_machine = “.”
domain_account = “woodgrove/kevinp” ‘ Information about the item to add
admin_group_name = “Administrators” ‘ the group in which to place that item

call add_to_local_group(this_machine, domain_account, admin_group_name )


 


 


Python Example

# —————————————-
# SCRIPT: add_user_to_local_group.py
#
# EXAMPLE: Add “domain\user1” to administrators group on local machine
# add_user_to_local_group.py “” domain\user1 Administrators
# add_user_to_local_group.py localhost domain\user1 Administrators
#
# NOTE: this is case-insensitive
# NOTE: requires win32 extensions for pythoon
#

import os
import win32net
import win32netcon
import sys

def add_user_to_local_group( computer, account, localgroup, ) :
localgroup = unicode(localgroup).lower()
groups = win32net.NetUserGetLocalGroups( computer, account, 0 )
groups = [ s.lower() for s in groups ]

if (localgroup in groups ) :
#user is a already in the group
pass
else :
#must add the user
level =3
members = []
members.append( {“domainandname” : account } )
win32net.NetLocalGroupAddMembers( computer, localgroup, level, members )

# postconditions
groups = win32net.NetUserGetLocalGroups( computer, account, 0 )
groups = [ s.lower() for s in groups ]
assert( localgroup in groups )

the_computer = sys.argv[1]
the_account_name = sys.argv[2]
the_group = sys.argv[3]

add_user_to_local_group( the_computer, the_account_name , the_group, )

# —————————————-


Saveen Reddy
2005-10-08


Comments (0)

Skip to main content