pythonpython-3.xpyad

Error trying to use pyad to update user account


I'm trying to update a user attribute in Active Directory using pyad. This is my code

from pyad import *

pyad.set_defaults(ldap_server="server.domain.local", 
username="admin", password="password")

pyad.adobject.ADObject.update_attribute(self='testuser1', attribute='mail', 
newvalue='my@email.com')

and this is the error i recieve.

Traceback (most recent call last):
 File "c:\Users\Administrator\Desktop\scripts\AD-Edit-user.py", line 12, in 
 <module>
 pyad.adobject.ADObject.update_attribute(self='testuser1', attribute='mail', 
 newvalue='my@email.com')
 File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-
 32\lib\site-packages\pyad-0.5.20-py3.6.egg\pyad\adobject.py", line 318, in 
 update_attribute
 elif pyadutils.generate_list(newvalue) != self.get_attribute(attribute):
 AttributeError: 'str' object has no attribute 'get_attribute'

This makes me assume that I need to change the attribute type from str to something else. I have verified that mail is the correct attribute name.

I know ldap connection is working because i can create a user using a similar script.


Solution

  • Your problem is how you use pyad. Specifically in this line:

    pyad.adobject.ADObject.update_attribute(self='testuser1', attribute='mail', 
    newvalue='my@email.com')
    

    if we look at the source of pyad.adobject.ADObject, we can see the following:

    def update_attribute(self, attribute, newvalue, no_flush=False):
        """Updates any mutable LDAP attribute for the object. If you are adding or removing
        values from a multi-valued attribute, see append_to_attribute and remove_from_attribute."""
        if newvalue in ((),[],None,''):
            return self.clear_attribute(attribute)
        elif pyadutils.generate_list(newvalue) != self.get_attribute(attribute):
            self._set_attribute(attribute, 2, pyadutils.generate_list(newvalue))
            if not no_flush:
                self._flush()
    

    self here is not a parameter of the function, it is a reference to the class instance. Your call includes self='testuser1' which is str.

    Please lookup the documentation on how to use this function / functionality / module, here. You will notice that there is no "self"... other than looking into the source code I am not sure how you got to the conclusion you needed self.

    I have no way of testing the following, but this is roughly how it should work:

    # first you create an instance of the object, based on 
    # the distinguished name of the user object which you 
    # want to change
    my_ad_object = pyad.adobject.ADObject.from_dn('the_distinguished_name')
    
    # then you call the update_attribute() method on the instance
    my_ad_object.update_attribute(attribute='mail', newvalue='my@email.com')