zakird / pyad

Python Active Directory Tools | *Not actively maintained*
http://zakird.github.io/pyad/
176 stars 72 forks source link

Access User Info and Querying Issue #114

Open gismcieri opened 5 years ago

gismcieri commented 5 years ago

Hi, I am having and issue with both getting the user and Querying AD both are returning errors. I am wondering if its a permissions issue or something. Any assistance would be greatly appreciated.

aduser error:

from pyad import aduser
user = aduser.ADUser.from_cn("myusername")
Traceback (most recent call last):

  File "<ipython-input-3-4a9ad300678e>", line 1, in <module>
    runfile('C:/Users/siteadmin/.spyder-py3/temp.py', wdir='C:/Users/siteadmin/.spyder-py3')

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/siteadmin/.spyder-py3/temp.py", line 2, in <module>
    user = aduser.ADUser.from_cn("siteadmin")

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\pyad\adobject.py", line 135, in from_cn
    return cls(adsearch.by_cn(cn, search_base, options), None, options)

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\pyad\adsearch.py", line 18, in by_cn
    return _ad_query_obj.get_single_result()['distinguishedName']

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\pyad\adquery.py", line 86, in get_single_result
    raise invalidResults(self.get_row_count())

invalidResults: The specified query resturned 0 results. getSingleResults only functions with a single result.

Query Error:

import pyad.adquery
q = pyad.adquery.ADQuery()

q.execute_query(attributes = ["distinguishedName", "description"], where_clause = "objectClass = '*'", base_dn = "OU=users, DC=domain, DC=com")

for row in q.get_results():
    print (row["distinguishedName"])
Traceback (most recent call last):

  File "<ipython-input-4-4a9ad300678e>", line 1, in <module>
    runfile('C:/Users/siteadmin/.spyder-py3/temp.py', wdir='C:/Users/siteadmin/.spyder-py3')

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/siteadmin/.spyder-py3/temp.py", line 9, in <module>
    q.execute_query(attributes = ["distinguishedName", "description"], where_clause = "objectClass = '*'", base_dn = "OU=users, DC=domain, DC=com")

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\pyad\adquery.py", line 78, in execute_query
    self.__rs, self.__rc = command.Execute()

  File "<COMObject ADODB.Command>", line 3, in Execute

  File "C:\Users\siteadmin\AppData\Local\ESRI\conda\envs\main\lib\site-packages\win32com\client\dynamic.py", line 287, in _ApplyTypes_
    result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)

com_error: (-2147352567, 'Exception occurred.', (0, 'Active Directory', 'A referral was returned from the server.\r\n', None, 0, -2147217865), None)

Thanks, Matt

seanwlk commented 5 years ago

Hi, about the aduser, is "siteadmin" the actual CN (Common Name) of the object or that's the sAMAccountName/ADUsername?. That function looks for the object in the given searchbase or whole domain in that case that has the CN equal to that. When you create an user in AD it asks you for Name and Surname and the CN is normally the sum of the two.

so for example if the name of my User is "John Smith", he's have the following attributes givenName = "John" sn = "Smith" cn = "John Smith" distinguishedName = "CN= John Smith, OU=Users, DC=my,DC=domain" even if his username is "siteadmin"

about the adquery, assuming you changed DC=xxxx in the query it should be working.