【问题标题】:Remove AD user from Security group using Python使用 Python 从安全组中删除 AD 用户
【发布时间】:2023-04-10 17:53:02
【问题描述】:

我正在尝试使用 Python 和 pywin32 从安全组中删除用户,但到目前为止还没有成功。但是,我可以将用户添加到安全组。

from win32com.client import GetObject

grp = GetObject("LDAP://CN=groupname,OU=groups,DC=blah,DC=local")

grp.Add("LDAP://CN=username,OU=users,DC=blah,DC=local") # successfully adds a user to the group

grp.Remove("LDAP://CN=username,OU=users,DC=blah,DC=local") # returns an error

错误如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject LDAP://CN=groupname,OU=groups,DC=blah,DC=local>", line 2, in Remove
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None,
 0, -2147024891), None)

我也尝试过使用 GetObject 添加来获取用户并以这种方式删除它,但是我得到了同样的错误。

usr = GetObject("LDAP://CN=user,OU=users,DC=blah,DC=local")

grp.Remove(usr)

任何帮助将不胜感激,因为我在这里遇到了死胡同。

编辑

我现在也尝试使用 Tim Golden 的 active_directory 模块来尝试删除组成员。

import active_directory as ad

grp = ad.find_group("groupname")
usr = ad.find_user("username")

grp.remove(usr.path())

但是这也不起作用,我遇到以下错误。

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\active_directory.py", line 799, in __getat
tr__
    attr = getattr(self.com_object, name)
AttributeError: 'PyIADs' object has no attribute 'group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\active_directory.py", line 802, in __getat
tr__
    attr = self.com_object.Get(name)
pywintypes.com_error: (-2147463155, 'OLE error 0x8000500d', (0, 'Active Director
y', 'The directory property cannot be found in the cache.\r\n', None, 0, -214746
3155), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\site-packages\active_directory.py", line 1081, in remove

    self.group.Remove(dn)
  File "C:\Python33\lib\site-packages\active_directory.py", line 804, in __getat
tr__
    raise AttributeError
AttributeError

编辑

Wherby 建议我改用 Python 2.7 并试一试。我刚刚试过这个:

import active_directory as ad

user = ad.find_user("username")
group = ad.find_group("groupname")

group.remove(user.path())

...但我仍然收到错误消息

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject LDAP://CN=groupname,OU=groups,DC=blah,DC=local>", line 2, in remove
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None,
 0, -2147024891), None)

用户和组肯定是正确找到的,因为我可以使用 print user.path()print group.path() 打印他们的 LDAP 路径

还有其他任何人都可以推荐的 Python 3.3 活动目录库吗?

【问题讨论】:

    标签: python active-directory pywin32


    【解决方案1】:

    来自

        Traceback (most recent call last):
      File "C:\Python33\lib\site-packages\active_directory.py", line 799, in __getat
    tr__
        attr = getattr(self.com_object, name)
    AttributeError: 'PyIADs' object has no attribute 'group'
    

    错误表明您使用的是不存在的“组名”,函数 find_group 需要一个存在的组名,但您给出的名称不存在。 您应该仔细检查“Tim Golden 的 active_directory 模块”的手册。

    对于

    usr = GetObject("LDAP://CN=user,OU=users,DC=blah,DC=local")
    
    grp.Remove(usr)
    

    建议你加“打印用户”,看看用户是否真的get到了。

    【讨论】:

    • 现有组名是什么意思?该组肯定存在,当我使用 find_group 获得它时,我可以打印它。我还可以打印用户。
    • 你的意思是在广告中你有一个名为“group”的组,还有一个名为“username”的用户?
    • 不...这些是我替换组/用户的实际名称的示例,因为它们无关紧要。
    • 话虽如此,引发的 AttributeError 根本没有被我编辑。错误字面意思是:“'PyIADs' 对象没有属性 'group'。”
    • 我注意到您使用的是 python 3.3,并且最新的 active_directory 模块是 2007 年构建的。我建议您更改为使用 python 2.7。在我的机器上,删除功能适用于 python 2.7。
    【解决方案2】:

    好吧,我走了,发现我有点像个木偶。我登录的帐户没有从 AD 组中删除的权限。当我以网络管理员帐户登录时,它就像一个魅力。

    最终代码:

    from win32com.client import GetObject
    
    group = GetObject("LDAP://CN=groupname,OU=Groups,DC=blah,DC=local")
    
    group.Remove("LDAP://CN=username,OU=Users,DC=blah,DC=local")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2021-11-21
      • 1970-01-01
      • 2019-10-31
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多