1

我正在尝试使用 pyad 更新 Active Directory 中的用户属性。这是我的代码

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

这是我收到的错误。

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'

这让我假设我需要将属性类型从更改为str其他类型。我已验证 mail 是正确的属性名称。

我知道 ldap 连接正在工作,因为我可以使用类似的脚本创建用户。

4

1 回答 1

0

你的问题是你如何使用pyad。具体在这一行:

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

如果我们查看pyad.adobject.ADObject 的来源,我们可以看到以下内容:

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这里不是函数的参数,它是对类实例的引用。您的电话包括self='testuser1'which is str

请在此处查找有关如何使用此功能/功能/模块的文档。您会注意到没有“自我”......除了查看源代码之外,我不确定您是如何得出您需要的结论的self

我无法测试以下内容,但这大致是它应该如何工作的:

# 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')
于 2018-02-14T19:33:47.083 回答