I sencond Adrej' comment about the filter. This is what works for me, I hope
my code doesn't make the real programmers shudder, I am just a low-ly
sysadmin. ;-)

#!/usr/bin/perl
# 05-30-2006    | initial draft | Luis E. Cerezo

use Net::LDAP;
$ldap = Net::LDAP->new( 'winDC' ) or die "$@";


$mesg = $ldap->bind( 'CN=Luis
Cerezo,OU=Users,OU=Houston,DC=onshore,DC=pgs,DC=com',
                        password => 'superdupersecretwindowspassword'
                        );

open (OUT, ">/tmp/somethingout.csv");
                        $mesg = $ldap->search (
filter=>"(&(objectclass=organizationalPerson)(objectclass=user)(!(objectclas
s=computer)))",
                        base=>"dc=onshore,dc=pgs,dc=com",
                        attrs=> ['sAMAccountName', 'name',
'userPrincipalName', 'displayName', 'distinguishedName'], );
                        $mesg->count();
                        if(! $mesg->count() )   { $err = $mesg->error();
print "$err\n"; }
                        @entries = $mesg->entries;
                                foreach $entry (@entries) {
                                                        @attrs =
$entry->attributes();
                                                        $upn   =
$entry->get_value(userPrincipalName);
                                                        $name  =
$entry->get_value(name);
                                                        $sam   =
$entry->get_value(sAMAccountName);
                                                        $DispName =
$entry->get_value(displayName);
                                                        $dn     =
$entry->get_value(distinguishedName);
                                                        print OUT
"$sam,$name,$DispName,$upn,$dn\n" ;
                                                        print "$dn\n";
                                                        }
close OUT;






On 8/12/08 1:07 PM, "John Arends" <[EMAIL PROTECTED]> wrote:

> I am trying to write a script to do some work with Active Directory.
> Since my scripts need to run on a Linux machine, I have to use Net::LDAP.
> 
> I have been going through previous postings and have found some useful
> information, but I am still having trouble. I took some code I wrote
> that talked to an OpenLDAP server and am trying to adapt it for our AD.
> I'm simply trying to bind to the AD and display some data since I want
> to make sure I can successfully bind and do some useful things before I
> get ahead of myself and try to write any additional code.
> 
> When I run the code I can tell I am binding because I don't get error
> messages (I do get an error if I put in a bogus server).
> 
> When I run the script I get no output. Simply nothing happens. Since I'm
> not getting any good diagnostic info, I'm having a hard time figuring
> out where to begin troubleshooting.
> 
> The OU SubOU has 1 test user account in it, as well as another OU. So I
> should be getting info on that one account.
> 
> Any help would be appreciated.
> 
> #!/usr/bin/perl
> 
> use Net::LDAP;
> 
> my $uid = "username";
> my $bindPass = "blah";
> my $ldapServer = "ldaps://ad.myorg.edu";
> 
> 
> $ldap = Net::LDAP->new ( $ldapServer ) or die "$@";
> 
> $ldap->bind($uid, password =>$bindPass);
> 
> $mesg = $ldap->search(filter=>"(OU=SubOU,OU=myOU)",
> base=>"dc=ad,dc=myorg,dc=edu");
> 
> @entries = $mesg->entries;
> foreach $entry (@entries) {
> 
>   print "Name: " . $entry->get_value("givenname") . "\n";
>   print "Email: " . $entry->get_value("mail") . "\n";
>   print "Phone: " . $entry->get_value("telephonenumber") . "\n";
> 
> }
> $ldap->unbind;
> 

-- 
Luis E. Cerezo
Global IT
+1 281 509 8397


This email and any files contained therein is confidential and may contain 
privileged information.  If you are not the named addressee(s) or you have 
otherwise received this in error, you should not distribute or copy this e-mail 
or use any of its content for any purpose. Please notify the sender immediately 
by e-mail if you have received this e-mail in error and delete it from your 
system

Reply via email to