Hi all,
I've been working on a script to search Active Directory for a user name and then return the homedirectory. Took me a while to figure out the correct LDAP string but I now have it running.
I'm using the name John Smith both to authenticate and as the test user to search for.
I have a list of about 2000 users who I need to run this against. So I need to read in the list and loop the main part of the script for each user. But I am having problems with some users who are not in Active Directory. Keep bombing out of the script with undefined variable errors when I get to the line:
Its the get_value which I cant seem to catch the error on and just replace the output a message to say the homedirectory wasnt found. So can anyone point me in the right direction please?
I've got the reading in the text file and running the loop working fine. Just the error is the problem.
I've been working on a script to search Active Directory for a user name and then return the homedirectory. Took me a while to figure out the correct LDAP string but I now have it running.
I'm using the name John Smith both to authenticate and as the test user to search for.
Code:
#!c:\perl\bin\perl -w
use Net::LDAP;
$ad = Net::LDAP->new('mycompany.com')||
die("Could not connect to LDAP server");
print "Connected OK!\n";
$ad->bind('CN=John Smith,OU=users,OU=europe,DC=mycompany,DC=com',
password=>'secret',
);
# Declare the necessary search variables
# What is the search base?
my $searchbase = 'OU=users,OU=europe,DC=mycompany,DC=com';
# What are we searching for?
my $filter = "CN=John Smith";
# Which attributes should be returned?
my $attrs = "cn, homeDirectory";
# Execute the search
my $results = $ad->search(base=>$searchbase,filter=>$filter,attrs=>$attrs);
# Display entries
my $entry;
$entry = $results->entry(0);
print $entry->get_value('cn').", ".$entry->get_value('homeDirectory')."\n";
# Unbind from the server
$ad->unbind;
print "Connection Dropped OK!\n";
I have a list of about 2000 users who I need to run this against. So I need to read in the list and loop the main part of the script for each user. But I am having problems with some users who are not in Active Directory. Keep bombing out of the script with undefined variable errors when I get to the line:
Code:
print $entry->get_value('cn').", ".$entry->get_value('homeDirectory')."\n";
I've got the reading in the text file and running the loop working fine. Just the error is the problem.