如何检查登录用户是否在Python中具有系统管理员权限

时间:2020-05-21 22:50:43

标签: python privileges elevated-privileges system-administration userinfo

我想检查当前登录的用户是否属于'Administrator'组的一部分,或者不在Python中。 我有一个仅需要为系统管理员启用功能的应用程序。我不想通过该操作的登录密码。以下是我的代码,用于检查系统管理员权限-

privilege = subprocess.check_output('whoami /groups', creationflags=CREATE_NO_WINDOW)
result = "S-1-5-32-544" in privilege
print privilege

现在,以上代码无需任何控制台窗口即可运行whoami.exe。但是该进程正在运行,因为Sophos阻止了whoami.exe。但是,如果我不使用whoami.exe,那么上面的代码将无法正常工作。

我尝试过的是-

#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

反正我可以解决此问题吗?注意:我在Windows 10计算机上使用Python 2.7

0 个答案:

没有答案