I want to check if the current logged on user is part of the 'Administrator' group or not in Python. I have an application for which I need to enable a feature for only System Admins. I do not want to pass the logged in password for the operation. Below is my code which works for checking the System admin privileges -
privilege = subprocess.check_output('whoami /groups', creationflags=CREATE_NO_WINDOW)
result = "S-1-5-32-544" in privilege
print privilege
Now, this above code runs whoami.exe without any console window. But the process is running since Sophos is blocking the whoami.exe. But if I do not use whoami.exe then the above code won't work.
What I have tried is-
#1. result = IsUserAnAdmin(); //Returns 'false' even if the logged in user is part of Administrator group
#2. import win32net
import getpass
username = getpass.getuser()
print username
info = win32net.NetUserGetInfo(None,username, 1) //pywintypes.error: (2221, 'NetUserGetInfo', 'The user name could not be found.')
print info
Is there anyway I can fix this issue? Note: I am using Python 2.7 on Windows 10 machine