Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to catch an error

Status
Not open for further replies.

skar

MIS
Mar 21, 2001
37
GB
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. :D
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";
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.
 
Thanks! That did the trick for the missing homeDirectory.
Code:
if($entry->exists('homeDirectory')) {
        $homedir = $entry->get_value('homeDirectory');
    } else {
        $homedir = "No HomeDirectory has been set.";
    }

I also did the same for the CN but its still failing with the same error. So, I'm still trying to catch the error for a user who isnt found.
As a side note: I did discover that our AD has some erm ... how should we say.. mistakes! We have a mix of users with (.) full stops between names and users without. So when I manage to catch the error of missing user I can try again by using the name with a (.) full stop between the names. Phew!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top