How do you move a user to a different OU using Python

早过忘川 提交于 2019-12-23 13:01:10

问题


I've been having a play with the fantastic active_directory module from Tim Golden and the extensive python-ldap module and while I see a great slew of articles on how to query, modify, create and delete entries within Active Directory from python I can't for the life of me find any advise on moving a user to a different OU using python. Is my google-foo failing me or is this not possible? (I've had great success with c# but I prefer to work in python where I can)

Thanks in advance

EDIT: OK, I've done some more digging and realized I need to be using the MODRDN command. This is provided through Python_Ldap so yay!...However I can't seem to coax python-ldap into authenticating using Windows credentials so I have been playing with pywin32. pywin32 is wonderful for editing attributes but I havent yet found a way to edit the distinguished name through this module...ho-hum! Any clues would be really appreciated.


回答1:


Ok I've solved it, and its rather nice too. This is a windows only solution I'm afraid as this uses the pywin32 module (though under python-ldap you have modrdn so you can solve it there too)

Ok Here is how to move user "jimboface" to OU "happyland"

import active_directory

user = active_directory.find_user("jimboface")
destination_ou = active_directory.find_ou("happyland")
destination_ou.com_object.MoveHere(str(user.as_string()), str(user.Name))
#Thats it!

Moments like this remind my why I love this language. Hope this helps someone!



来源:https://stackoverflow.com/questions/4678748/how-do-you-move-a-user-to-a-different-ou-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!