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;

In reply to Re^2: Match csv to AD and returning email addresses by meredib
in thread Match csv to AD and returning email addresses by meredib

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.