0

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

Soumya1008
  • 31
  • 4
  • Does this answer your question? [Python - checking if a user has administrator privileges](https://stackoverflow.com/questions/2946746/python-checking-if-a-user-has-administrator-privileges) – Umar.H May 21 '20 at 22:57
  • first answer in dupe as you're using windows – Umar.H May 21 '20 at 22:58
  • Not really. Actually I am a domain admin and the userid is a part of local admin group. So UAC stops me to get access to temp folder. I want to check if I am a part of local admin group or not through python. Can you please help? – Soumya1008 May 22 '20 at 10:39

0 Answers0