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

2019-06-23 22:59发布

问题:

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!