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

I have been working on a script for a few days now and I'm just not ge +tting the handle on it. One of the things I'm trying to do is read i +n a csv file that has a list of names in two columns like: "Johnson, Joe",Phyllis Ivy "Harris, Al",Maureen Murl "Farris, Allen",Sandra Norm "Daniels, Greg",Gwen Fair Next, I search AD and return their email addresses. Of course some th +em won't be found which is another problem I'm having. So since it returns some of the emails then the ones that it doesn't r +eturn it messes up the formatting. Also, what I want it to do is ret +urn the names of the people if an email address was not found. please let me know if anyone needs more information. below is the script that I am using currently: #!perl use Getopt::Long; use Net::LDAPS; use Text::CSV; ($domainCont, $surNm, $givenNm, $dispNm, $eNo, $outFlNm, $id, $pw) = ( +'', '', '','', -1, '', '', ''); &GetOptions('dc=s' => \$domainCont ,'i=s' => \$id ,'p=s' => \$pw ,'d=s' => \$baseDir ,'f=s' => \$inFlNm); my $ad = Net::LDAP->new($domainCont) or die "Can't connect to AD >$!<\ +n"; $ad->bind($id, password=>$pw) or die " bind for $id failed >$!<\n"; my $base = 'OU=People,OU=staff,DC=ad,DC=wjgilmore,DC=com'; my $attrs = "sn, givenname"; open IN, "<"."./$baseDir/$inFlNm" or die "Can't open transfers file >$ +inFlNm<\n"; $outFlNm = "test.csv"; open OUT, ">"."./$baseDir/$outFlNm" or die "Can't open transfers file +>$outFlNm<\n"; while(<IN>){ @manager = split(/"/, $_); @assist = split(/ /, $manager[2]); chop($assist[1]); @assist2 = split(/,/, $assist[0]); $assistant = "$assist[1], $assist2[1]"; $filter1 = '(displayName='.$manager[1].')'; $filter2 = '(displayName='.$assistant.')'; my $results1 = $ad->search(base=>$base,filter=>$filter1,attrs=>$attrs) +; my $results2 = $ad->search(base=>$base,filter=>$filter2,attrs=>$attrs) +; #print "$filter1\n"; foreach $adEnt ($results1->entries){ my $entry1 = $results1->entry($i1); my $mail1 = $entry1->get_value('mail'); chomp; print OUT "$manager[1],"; print OUT "$mail1,"; #printf $entry1->get_value('mail')."\n"; } foreach $adEnt ($results2->entries){ my $entry2 = $results2->entry($i2); my $mail2 = $entry2->get_value('mail'); print OUT "$assistant,"; print OUT "$mail2,\n"; #printf $entry2->get_value('mail')."\n"; } } $ad->unbind;

Replies are listed 'Best First'.
Re: Match csv to AD and returning email addresses
by apl (Monsignor) on Jan 21, 2009 at 16:24 UTC
    So since it returns some of the emails then the ones that it doesn't return it messes up the formatting.
    Then why don't you either fix the formatting (whatever that is) or not produce whatever it is that requires formatting when you don't get an e-mail address?
    Also, what I want it to do is return the names of the people if an email address was not found.
    Why don't you?

    I'm afraid I really don't understand what your problem is.

      Ok I almost have it; however, when I run the script shown below when it finds more than one user with the same name it just prints one email address for each one and not each one for each user.
      For instance: It returns: User - Email Smith, Joe - Joe.Smith@perlmonks.com Smith, Joe(2) - Joe.Smith@perlmonks.com instead of: User - Email Smith, Joe - Joe.Smith@perlmonks.com Smith, Joe(2) - Joe.Smith2@perlmonks.com
      #!perl use Getopt::Long; use Net::LDAPS; use Text::CSV; ($domainCont, $surNm, $givenNm, $dispNm, $eNo, $outFlNm, $id, $pw) = ( +'', '', '','', -1, '', '', ''); &GetOptions('dc=s' => \$domainCont ,'i=s' => \$id ,'p=s' => \$pw ,'d=s' => \$baseDir ,'f=s' => \$inFlNm); my $ad = Net::LDAP->new($domainCont) or die "Can't connect to AD >$!<\ +n"; $ad->bind($id, password=>$pw) or die " bind for $id failed >$!<\n"; my $base = 'OU=People,OU=staff,DC=ad,DC=wjgilmore,DC=com'; my $attrs = "sn, givenname"; open IN, "<"."./$baseDir/$inFlNm" or die "Can't open transfers file >$ +inFlNm<\n"; $outFlNm = "test.csv"; open OUT, ">"."./$baseDir/$outFlNm" or die "Can't open transfers file +>$outFlNm<\n"; print OUT "Name,,,Assistant\n"; while(<IN>){ @mngr = split(/"/, $_); @manager = split(/, /, $mngr[1]); $mFName = "$manager[1]"; $mLName = "$manager[0]"; @assist = split(/,/, $mngr[2]); @assistant = split(/ /, $assist[1]); chomp($assistant[1]); $aFName = "$assistant[0]"; $aLName = "$assistant[1]"; $lenMFName = length($manager[1])*.5; $lenAFName = length($assistant[0])*.5; substr($manager[1],-$lenMFName)=$pmFName; substr($assistant[0],-$lenAFName)=$paFName; $filter1 = '(&(sn='.$mLName.')(givenname='."$manager[1]*".'))'; $filter2 = '(&(sn='.$aLName.')(givenname='."$assistant[0]*".'))'; my $results1 = $ad->search(base=>$base,filter=>$filter1,attrs=>$at +trs); my $results2 = $ad->search(base=>$base,filter=>$filter2,attrs=>$at +trs); $x1 = $results1->count(); $x2 = $results2->count(); if($x1 > 0){ foreach $adEnt ($results1->entries){ my $entry1 = $results1->entry($i1); my $mail1 = $entry1->get_value('mail'); foreach $adEnt ($results2->entries){ print OUT "$mFName,$mLName,"; print OUT "$mail1,"; my $entry2 = $results2->entry($i2); my $mail2 = $entry2->get_value('mail'); print OUT "$aFName,$aLName,"; print OUT "$mail2,\n"; } } } else { if($x2 > 0) { print OUT "$mFName,$mLName,Not Found,"; foreach $adEnt ($results2->entries){ my $entry2 = $results2->entry($i2); my $mail2 = $entry2->get_value('mail'); print OUT "$aFName,$aLName,"; print OUT "$mail2,\n"; } } else { print OUT "$mFName,$mLName,Not Found,$aFName,$aLName,Not F +ound"; } } } $ad->unbind;
        First I'd add use strict; use warning;. At the very least, $i2 is not defined.

        Second, I'd add a "\n" at the end of print OUT "$mail1,";

        Third, I don't see how the code you posted writes what you say it did. (I don't, for example, see hyphens in any of your prints.)