def create_local_student_account(user_name, full_name, password): global STUDENTS_GROUP # Ensure the Students group exists ret = create_local_students_group() # Create local student account student = None try: print("\tAdding student account...") accounts.User.create(user_name, password) except pywintypes.error as err: if err[2] == "The account already exists.": pass else: # Unexpected error print(str(err)) ret = False # Get the student object student = accounts.user(user_name) # Set properties for this student # win32net.NetUserChangePassword(None, user_name, old_pw, password) user_data = dict() user_data['name'] = user_name user_data['full_name'] = full_name user_data['password'] = password user_data[ 'flags'] = win32netcon.UF_NORMAL_ACCOUNT | win32netcon.UF_PASSWD_CANT_CHANGE | win32netcon.UF_DONT_EXPIRE_PASSWD | win32netcon.UF_SCRIPT user_data['priv'] = win32netcon.USER_PRIV_USER user_data['comment'] = 'OPE Student Account' # user_data['home_dir'] = home_dir # user_data['home_dir_drive'] = "h:" user_data['primary_group_id'] = ntsecuritycon.DOMAIN_GROUP_RID_USERS user_data['password_expired'] = 0 user_data['acct_expires'] = win32netcon.TIMEQ_FOREVER win32net.NetUserSetInfo(None, user_name, 3, user_data) # Add student to the students group print("\tAdding student to students group...") grp = accounts.LocalGroup(accounts.group(STUDENTS_GROUP).sid) try: grp.add(student) except pywintypes.error as err: if err[2] == "The specified account name is already a member of the group.": pass else: # Unexpected error print(str(err)) ret = False # # home_dir = "%s\\%s" % (server_name, user_name) # return ret
def add_user_to_group(user_name, group_name): try: # Get the group grp = accounts.LocalGroup(accounts.group(group_name).sid) # Get the user user = accounts.user(user_name) grp.add(user) except Exception as ex: if ex.args[ 2] == "The specified account name is already a member of the group.": pass else: p("}}rbERROR - Unexpected exception trying to add user to group (" + \ user_name + "/" + group_name + "\n}}xx" + str(ex)) return False return True
def create_local_admin_account(user_name, full_name, password): # Create local admin account ret = True admin = None try: p("}}yn\tAdding Admin account (" + user_name + ")...}}xx") accounts.User.create(user_name, password) # p("}}yn\t\tDone.}}xx") # except pywintypes.error as err: except Exception as err: if err.args[2] == "The account already exists.": pass else: # Unexpected error p("}}rb" + str(err) + "}}xx") ret = False # Get the student object admin = accounts.user(user_name) # Set properties for this student # win32net.NetUserChangePassword(None, user_name, old_pw, password) user_data = dict() user_data['name'] = user_name user_data['full_name'] = full_name user_data['password'] = password user_data[ 'flags'] = win32netcon.UF_NORMAL_ACCOUNT | win32netcon.UF_PASSWD_CANT_CHANGE | win32netcon.UF_DONT_EXPIRE_PASSWD | win32netcon.UF_SCRIPT user_data['priv'] = win32netcon.USER_PRIV_ADMIN user_data['comment'] = 'OPE Admin Account' # user_data['home_dir'] = home_dir # user_data['home_dir_drive'] = "h:" user_data['primary_group_id'] = ntsecuritycon.DOMAIN_GROUP_RID_USERS user_data['password_expired'] = 0 user_data['acct_expires'] = win32netcon.TIMEQ_FOREVER win32net.NetUserSetInfo(None, user_name, 3, user_data) # Add user to the required groups p("}}yn\tAdding user to Administrators group...}}xx") grp = accounts.LocalGroup(accounts.group("Administrators").sid) users_grp = accounts.LocalGroup(accounts.group("Users").sid) try: # Add to administrators group grp.add(admin) # except pywintypes.error as err: except Exception as err: if err.args[ 2] == "The specified account name is already a member of the group.": pass else: # Unexpected error p("}}rb" + str(err) + "}}xx") ret = False try: # Add to users group users_grp.add(admin) # except pywintypes.error as err: except Exception as err: if err.args[ 2] == "The specified account name is already a member of the group.": pass else: # Unexpected error p("}}rb" + str(err) + "}}xx") ret = False return ret