win32net无法从组中删除本地用户
一个简单的简单任务遇到问题...找到不属于本地管理员组的用户并将其删除...
import win32net
def BAD_DomainUsers(computer):
x = win32net.NetLocalGroupGetMembers(computer,"Administrators", 2)
for i in x[0]:
if i["domainandname"] == r"DOMAIN\Domain Users":
return True
return False
def Remove_BadUsers(computer):
win32net.NetLocalGroupDelMembers(computer, "Administrators", r"DOMAIN\Domain Users")
computer = "P04213"
if BAD_DomainUsers(computer): Remove_BadUsers(computer)
这会返回错误:
win32net.NetLocalGroupDelMembers(computer, "Administrators", r"DOMAIN\Domain Users")
pywintypes.error: (1387, 'NetLocalGroupDelMembers', 'A member could not be added to or removed from the local group because the member does not exist.')
但是当我枚举管理员组时,果然 DOMAIN\Domain Users 是成员...否则它不会调用Remove_BadUsers函数。一定有什么东西我错过了,但我无法弄清楚。
Having a problem with a simple simple task... find users that don't belong in the local administrators group and remove them...
import win32net
def BAD_DomainUsers(computer):
x = win32net.NetLocalGroupGetMembers(computer,"Administrators", 2)
for i in x[0]:
if i["domainandname"] == r"DOMAIN\Domain Users":
return True
return False
def Remove_BadUsers(computer):
win32net.NetLocalGroupDelMembers(computer, "Administrators", r"DOMAIN\Domain Users")
computer = "P04213"
if BAD_DomainUsers(computer): Remove_BadUsers(computer)
This returns the error:
win32net.NetLocalGroupDelMembers(computer, "Administrators", r"DOMAIN\Domain Users")
pywintypes.error: (1387, 'NetLocalGroupDelMembers', 'A member could not be added to or removed from the local group because the member does not exist.')
But when I enumerate the Administrators group, sure enough DOMAIN\Domain Users is a member... or else it would not call the Remove_BadUsers function. There must be SOMETHING I am missing, but I can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很清楚这篇文章很旧,但它是搜索中的最高结果,我讨厌不回答所提出问题的答案。所以,答案如下:
win32net.NetLocalGroupDelMembers 需要一个字符串列表作为它的第三个参数
例如 win32net.NetLocalGroupDelMembers(computer, "Administrators", [r"DOMAIN\Domain Users"])
I'm well aware that this post is old, but it is the top result in searches and I hate answers that don't answer the question posed. So, here is the answer:
win32net.NetLocalGroupDelMembers expects a list of strings for it's third argument
e.g. win32net.NetLocalGroupDelMembers(computer, "Administrators", [r"DOMAIN\Domain Users"])
好吧……
这已经是多虑了。答案是使用子进程或某种方法来调用命令行语句,而命令行语句是...
我刚刚在cmd提示符下执行了该命令,并且它运行成功。经典的。
Ok...
This has been overthought. The answer is to use subprocess or some method to invoke a command line statement and the command line statement is...
I just did the command in a cmd prompt and it ran successfully. Classic.