使用Python从安全组中删除AD用户

使用Python从安全组中删除AD用户,python,active-directory,pywin32,Python,Active Directory,Pywin32,我试图使用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 t

我试图使用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)
任何帮助都将不胜感激,因为我在这里遇到了死胡同

编辑

我现在还尝试使用Tim Golden的active_目录模块尝试删除组成员

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
。。。但我还是犯了个错误

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)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
删除中第2行的文件“”
pywintypes.com_错误:(-2147352567,‘发生异常’,(0,无,无,
0,-2147024891),无)
用户和组肯定是正确的,因为我可以使用
print user.path()
print group.path()

有没有其他人可以推荐用于Python 3.3的active directory库?

    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_目录模块”的手册

为了


我建议你加上“打印用户”,看看用户是否真的得到了它。

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

最终代码:

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

您所说的现有组名是什么意思?该组确实存在,当我使用find_group获得它时,我可以打印它。我还可以打印用户。你是说在广告中你有一个名为“组”的组,还有一个名为“用户名”的用户?不。。。在这些例子中,我替换了组/用户的实际名称,因为它们不相关。尽管如此,我根本没有编辑引发的AttributeError。错误的字面意思是:“'PyIADs'对象没有'group'属性。”我注意到您使用的是python 3.3,最新的ActiveDirectory模块是在2007年之前构建的。我建议您改为使用python 2.7。在我的机器上,remove函数适用于python 2.7。
    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'
usr = GetObject("LDAP://CN=user,OU=users,DC=blah,DC=local")

grp.Remove(usr)
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")