Skip to content

Instantly share code, notes, and snippets.

@r4vi
Created April 12, 2011 11:10
Show Gist options
  • Save r4vi/915335 to your computer and use it in GitHub Desktop.
Save r4vi/915335 to your computer and use it in GitHub Desktop.
checks if the current logged in user is removed from the local administrators group and re-adds. (windows)
import ctypes
import win32net, win32api
import time
current_user = win32api.GetUserNameEx(2)
data = [{"domainandname": current_user}]
def get_members(group_name):
members = win32net.NetLocalGroupGetMembers(None, group_name, 3)[0]
print members
return [x['domainandname'].lower() for x in members]
def is_user_in_group(username, groupname):
return username.lower() in get_members(groupname)
print 'checking if current user %s has admin rights' % (current_user,)
while True:
if (is_user_in_group(current_user, 'Administrators')):
pass # user is already an admin
else:
print 'User has been removed from local administrators. Re-adding'
if (not is_user_in_group(current_user, 'Administrators')):
win32net.NetLocalGroupAddMembers(None, 'Administrators', 3, data)
print 'SUCCESS'
else:
print 'FAILED'
time.sleep(10)
@r4vi
Copy link
Author

r4vi commented Apr 12, 2011

the idea being that you just run this forever. When an automated script removes you, it adds you back. this is because you actually only lose your administrator status when you log-in again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment