in reply to Re: Match csv to AD and returning email addresses
in thread Match csv to AD and returning email addresses

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;

Replies are listed 'Best First'.
Re^3: Match csv to AD and returning email addresses
by apl (Monsignor) on Jan 28, 2009 at 21:07 UTC
    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.)

      Ok I added strict and warning and I just removed $i1 and $i2 from where they were. I do not need a \n due to how I want the output displayed. There are hyphens all over the place, if you don't see a hyphen at the end of the line see the next line and if there is a + in the front of the line it means that it is a continuation of the previous line.