【问题标题】:Error trying to use pyad to update user account尝试使用 pyad 更新用户帐户时出错
【发布时间】:2018-02-14 18:51:29
【问题描述】:

我正在尝试使用 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 连接工作正常,因为我可以使用类似的脚本创建用户。

【问题讨论】:

  • 你能用完整的 Trace 更新你的问题吗?
  • @Edwin van Mierlo 完成

标签: python python-3.x pyad


【解决方案1】:

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

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

如果我们查看source of 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',即str

请查阅有关如何使用此功能/功能/模块的文档,here。你会注意到没有“自我”......除了查看源代码我不确定你是如何得出你需要的结论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')

【讨论】:

  • 感谢您的回复。我最初将 self 排除在外,但收到以下消息: Traceback(最近一次调用最后一次): pyad 中的文件“c:\Users\Administrator\Desktop\scripts\AD-Edit-user.py”,第 12 行。 adobject.ADObject.update_attribute(attribute='mail', newvalue='my@email.com') TypeError: update_attribute() missing 1 required positional argument: 'self'
  • 是的,这是意料之中的,因为您没有创建对象的实例,请参阅我更新的答案。我无法对此进行测试。而且我找不到任何示例代码......但是时间用完了......祝你好运。
  • 我仍然收到一个错误,我还没有时间研究它,但我想回来感谢您的帮助以及您回答中的详细信息!
  • 我收到的最后一个错误是由于 DN 错误我使用 ou=users 而我应该使用 cn=users 再次感谢您的帮助。 my_ad_object = pyad.adobject.ADObject.from_dn("cn=testuser1,cn=Users,dc=domain,dc=local") my_ad_object.update_attribute(attribute='mail', newvalue='my@email.com')跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
相关资源
最近更新 更多