2015-05-27 65 views
7

我想從使用Python和pywin32的安全組中刪除用戶,但到目前爲止還沒有成功。不過,我可以將用戶添加到安全組。使用Python從安全組中刪除AD用戶

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) 

任何幫助將非常感謝,因爲我在這裏遇到了一個死衚衕。

編輯

我也用添金的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()打印他們的LDAP路徑和print group.path()

是否有任何人可以推薦的Python 3.3的其他活動目錄庫?

回答

0

那麼,我走了,發現我是一個混蛋。我登錄的帳戶沒有從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") 
0

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' 

錯誤指示您正在使用不存在「組名」,需要anexisted組名稱的功能find_group,但你給不存在的名稱。 您應該仔細檢查「Tim Golden的active_directory模塊」的手冊。

對於

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

grp.Remove(usr) 

我建議你添加「打印用戶」,看看用戶是否真正得到。

+0

你說的一個已經存在的組名是什麼意思?該組絕對存在,當我用find_group得到它時,我可以打印它。我也可以打印用戶。 – ryansin

+0

您的意思是在廣告中您有一個名爲「group」的組,並且也有一個名爲「username」的用戶? – wherby

+0

否...這些都是我替換組/用戶的實際名稱的例子,因爲它們無關緊要。 – ryansin