Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using Net::LDAP to search Active Directory for a certain user ID and return their cn and lastlogintime attributes. Here is my code:
foreach my $uid (keys %results){ $search = $ad_ldap->search( base => 'DC=company,DC=domain,DC=lan', scope => 'sub', filter => "(&(cn=".$uid.")(objectclass=user))", attrs => ['lastlogon', 'cn'] ); # if no results were returned, set ad_lastlogintime to 0 and g +o to next uid if ( $search->count == 0){ $results{$uid}{ad_lastlogintime}{0} = 1; next; } # store the AD last login time in the uid hash foreach my $entry ($search->entries){ $results{$uid}{ad_lastlogintime}{$entry->get_value('lastlo +gon')} = 1; } }
The problem is this: there is a uid in AD that when I search for it, the search does not return anything. $search->count is equal to 0, so I just set the lastlogintime to 0. The ID that I am searching for is definitely in AD but for some reason it does not find it at all when it searches for it. We think the issue may be the possibility that the ID is actually a reference. From what it looks like, I might be able to get the correct result if I just don't try to dereference the object (ID) when I search for it, however I don't know how to do this. I realize that are several Net::LDAP modules that deal with this and I looked at them but I really don't understand them. If anyone has any ideas, I would appreciate a little help. Also, I know the information is a little sketchy here so if you need more, please let me know. Thanks!
  • Comment on Can't get correct returns when querying Active Directory with Net::LDAP
  • Download Code

Replies are listed 'Best First'.
Re: Can't get correct returns when querying Active Directory with Net::LDAP
by shmem (Chancellor) on Mar 06, 2009 at 21:58 UTC

    Your bind() does succeed, I assume? Try searching with the sAMAccountName attribute instead of cn.

      Thanks for the reply.

      My bind does work. The problem is not that my search does not return the attributes that I am asking for, but rather, the search does not even find the ID I am looking for in the first place even though the ID definitely does exist in Active Directory.

      If any one has any insight into this, I would appreciate it. I think the problem has something to do with referals/references but I don't understand it.

      Thanks!