Created
June 6, 2016 00:46
-
-
Save vlad-bezden/1fe5b5d4f170241cbe6522593c4aec86 to your computer and use it in GitHub Desktop.
Program adds user to the ADMINISTRATORS group every 60 seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32net | |
import sys | |
import time | |
USER = 'some_user_name' | |
GROUP = 'ADMINISTRATORS' | |
DOMAIN = 'some_domain' | |
CHECK_TIME = 60 # delay for 60 seconds | |
USER_PART_OF_THE_GROUP_ERROR_NUMBER = 1378 | |
user_group_info = { | |
'domainandname': '{}\\{}'.format(DOMAIN, USER) | |
} | |
# | |
# adds user to the group | |
# | |
def add_user_to_group(): | |
try: | |
win32net.NetLocalGroupAddMembers(None, GROUP, 3, [user_group_info]) | |
except win32net.error as error: | |
number = error.args[0] | |
if number != USER_PART_OF_THE_GROUP_ERROR_NUMBER: | |
print('Error Number: \'{}\'; Context: \'{}\'; Message: \'{}\''.format(*error.args)) | |
raise | |
# | |
# Checks if user is part of the group | |
# | |
def if_user_in_group(): | |
members = win32net.NetLocalGroupGetMembers(None, GROUP, 1) | |
return USER.upper() in list(map(lambda d: d['name'].upper(), members[0])) | |
# | |
# main loop that runs a program | |
# | |
def main_loop(): | |
while True: | |
if if_user_in_group(): | |
print_with_timestamp("'{}' is ALREADY part of the '{}' group".format(USER, GROUP)) | |
else: | |
add_user_to_group() | |
print_with_timestamp("'{}' is ADDED to the '{}' group".format(USER, GROUP)) | |
time.sleep(CHECK_TIME) | |
# | |
# timestamp | |
# | |
def print_with_timestamp(message): | |
print('{} - {}'.format(time.strftime('%a %H:%M:%S'), message)) | |
if __name__ == '__main__': | |
try: | |
main_loop() | |
except KeyboardInterrupt: | |
print('Exiting by user request') | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment