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

I'm grabbing an email address that is in a different OU that what I'm writing to. I am able to grab this email address but I don't know how to add it to my ldap entry. How do I get the address that I'm printing added to my entry?
$entry = $AD_conn->search($adld{root}, $adld{scope}, "(&(|(employ +eeID=$UMF))(mail=*))", 0, @attr); if (!$entry) { my $code = $AD_conn->getErrorCode(); if ($code == 0) { print "No Directory records found to process on this run.\n" } else { print "Run terminated on LDAP Error=" . $AD_conn->getErrorStri +ng() . ".\n"; } #$AD_conn->close(); } else { while($entry) { print "employeeID mail: $entry->{mail}[0],\n"; $entry = $AD_conn->nextEntry(); } # End of while loop } my $ID = $entry->{mail}[0]; # Create the new Directory entry # $entry = new Mozilla::LDAP::Entry; $entry->setDN(("cn=$CN,OU=FunctionalContacts,OU=Functional,OU=Exch +ange,OU=Services,") . $adld{root}); $entry->addValue("objectclass", "top"); $entry->addValue("objectclass", "organizationalPerson"); $entry->addValue("objectclass", "person"); $entry->addValue("objectclass", "contact"); $entry->addValue("description", "POC:$ID"); $fa_values{"mailnickname"} = $username; while (($attr,$value) = each %fa_values) { $entry->addValue($attr, $value); } $AD_conn->add($entry); if ($AD_conn->getErrorCode()) { print "FAILED create_record:$entry->{mailNickname}[0] because '" . + $AD_conn->getErrorString() . "'\n\n"; } else { print "Created mailNickname:$entry->{mailNickname}[0]\n"; } }
I'm getting this error: Can't use string ("") as a HASH ref while "strict refs" in use at ./functionals.pl line 384.

which is: my $ID = $entry->{mail}[0];

What do I need to do to be able to add the mail address that is being retrieved into the description?

output from print:
employeeID mail: jim.doe@xxxx.com,
employeeID mail: john.smith@xxxx.com,
employeeID mail: mark.jones@xxxx.com,
employeeID mail: mark.jones@xxxx.com,
No Directory records found to process on this run.
employeeID mail: john.doe@xxxx.com,

Replies are listed 'Best First'.
Re: Adding attribut to AD
by oeuftete (Monk) on Nov 25, 2008 at 18:35 UTC

    Once it gets to the line you indicated, $entry is going to be some sort of emptiness or false... either you hit on ( !$entry ) in your if, or your while($entry) was false because that's how it was set in

    entry = $AD_conn->nextEntry();

    And indeed the error indicates that $entry == "" when you try to dereference it.

    So... maybe try creating your new entry within the while loop?

    while ($entry ) { # Create the new Directory entry # my $new_entry = new Mozilla::LDAP::Entry; # etc. $entry = $AD_conn->nextEntry(); } # End of while loop
      thank you oeuftete. I was able to get it to work like this:
      $entry = $AD_conn->search($adld{root}, $adld{scope}, "(&(|(employe +eID=$UMF))(mail=*))", 0, @attr); if (!$entry) { my $code = $AD_conn->getErrorCode(); if ($code == 0) { print "No Directory records found to process on this run.\n" } else { print "Run terminated on LDAP Error=" . $AD_conn->getErrorStri +ng() . ".\n"; } } else { while($entry) { print "employeeID mail: $entry->{mail}[0],\n"; my $ID = $entry->{mail}[0]; # Create the new Directory entry # $entry = new Mozilla::LDAP::Entry; $entry->setDN(("cn=$CN,OU=FunctionalContacts,OU=Functional,OU=Exch +ange,OU=Services,") . $adld{root}); $entry->addValue("objectclass", "top"); $entry->addValue("objectclass", "organizationalPerson"); $entry->addValue("objectclass", "person"); $entry->addValue("objectclass", "contact"); $entry->addValue("description", "POC:$ID"); # Put the remaining values into the AD Directory $fa_values{"mailnickname"} = $username; while (($attr,$value) = each %fa_values) { $entry->addValue($attr, $value); } $AD_conn->add($entry); if ($AD_conn->getErrorCode()) { print "FAILED create_record:$entry->{mailNickname}[0] because '" . + $AD_conn->getErrorString() . "'\n\n"; } else { print "Created mailNickname:$entry->{mailNickname}[0]\n"; } $entry = $AD_conn->nextEntry(); } # End of while loop }
      One thing I did notice though with this is if their is not a functional number then it just skips over that functional and doesn't create an account at all.
      I need it to still add functionals even though they don't have a functional number.
        I got it working thank you again oeuftete