×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Enumerate AD
3

Enumerate AD

Enumerate AD

(OP)
Hello Everyone

I wrote a Perl script to gather NIS and Active directory info. The issue I am having is the only way I have been able to enumerate AD is with this snippet. The $sNisInfo contains the "userID:GrpName:UserRealName:" from NIS.

$result = $ldap->search(
base => "dc=mycompany,dc=com",
filter => "sAMAccountName=$sZid",
);
# die $result->error if $result->code;
foreach my $entry ($result->entries) {
printf OUTPUT "$sNisInfo:%s:%s:%s:%s\n",
$entry->get_value("displayName"),
$entry->get_value("telephoneNumber"),
$entry->get_value("l"),
$entry->get_value("mail");
}

I get an error whenever a user ID in AD does not have the info I am searching for filled out I get "Use of uninitialized value in printf". How do I get printf to just leave the value blank if it is empty and not error out. Thanks in advance for any help.

Randy

RE: Enumerate AD

Check it first

CODE --> perl

if ($str eq "")
{
  // ...
} 


CODE --> perl

if ($str ne "")
{
  // ...
} 

EDIT:
Probably best had I posted the right code first!


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Enumerate AD

(OP)
Thank you Chris for your help. I am not sure where to put the "if" statement I tried putting it around all the code in the "foreach" loop but I get the same results without the "if" statement as I originally got. I then tried to put the "if" statement around the "printf" statement but then I get "Use of uninitialized value in printf" on all the results no data is returned at all.

Randy

RE: Enumerate AD

It depends which you are going to use.

A: if ... eq "" is to check that the variable has no value so is eqal to an empty string.


B: if ... ne "" is to check that the variable has a value so is not equal to an empty string

and the statement you want to run if the comparison returns 'true' goes where the // ... is in the example


if ($str ne "")
{
print "Is NOT an empty string."
}

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Enumerate AD

Chris, the test if($str ne ""){} checks if $str is an empty string, but NOT if $str is undef (uninitialized). That's why Randy continues to see the error. A better test would be if($str){}, though this one is also not perfect, as a value $str="0" returns false. Possibly the best one is if(length$str){}.
Randy, you could say no strict just before the if statement around the printf statement and restore use strict after it (if you want to take the risk), or say

CODE -->

$displayName=$entry->get_value("displayName");
$displayName=""unless length$displayName; 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Enumerate AD

You could always use ternary to evaluate the value

CODE

printf OUTPUT "$sNisInfo:%s:%s:%s:%s\n",
((defined $entry->get_value("displayName"))?$entry->get_value("displayName"):""),
((defined $entry->get_value("telephoneNumber"))?$entry->get_value("telephoneNumber"):""),
((defined $entry->get_value("l"))?$entry->get_value("l"):""),
((defined $entry->get_value("mail"))?$entry->get_value("mail"):""); 

Perl IMO is a bit ugly when trying to handle undefined values.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Enumerate AD

(OP)
It's working thank you both for your help. Thanks again.

Randy

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close