win32net无法从组中删除本地用户

发布于 2025-01-01 15:22:33 字数 887 浏览 0 评论 0原文

一个简单的简单任务遇到问题...找到不属于本地管理员组的用户并将其删除...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2025-01-08 15:22:33

我很清楚这篇文章很旧,但它是搜索中的最高结果,我讨厌不回答所提出问题的答案。所以,答案如下:

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"])

明月松间行 2025-01-08 15:22:33

好吧……

这已经是多虑了。答案是使用子进程或某种方法来调用命令行语句,而命令行语句是...

net localgroup administrators "DOMAIN\Domain Users" /delete

我刚刚在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...

net localgroup administrators "DOMAIN\Domain Users" /delete

I just did the command in a cmd prompt and it ran successfully. Classic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文