http://www.perlmonks.org?node_id=544195

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

Is there a way I could write a script to grab this data? There is a .Net developer in my group who access AD but doesnt use DC and OU. Instead he uses DirectoryEntry .NET class to access AD. Any help would be greatly appreciated? Todd

Replies are listed 'Best First'.
Re: Active Directory DC and OU
by Argel (Prior) on Apr 18, 2006 at 23:59 UTC
      Thanks for everyones help... All this information really helped.....
Re: Active Directory DC and OU
by idsfa (Vicar) on Apr 18, 2006 at 21:37 UTC

    <psychic>

    You can query an active directory's LDAP entries using the perl modules Net::LDAP or Net::LDAP::Express.Example:

    use Net::LDAP; $ldap = Net::LDAP->new( 'pdc.foobar.com' ) or die "$@"; $mesg = $ldap->bind ; # an anonymous bind $mesg = $ldap->search( # perform a search base => "c=US", filter => "(&(cn=Some Loser))" ); $mesg->code && die $mesg->error; foreach $entry ($mesg->entries) { $entry->dump; } $mesg = $ldap->unbind; # take down session

    </psychic>


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon

      If you just want to query ActiveDirectory, Net::LDAP is fine. But for writing, AD seems to have a problem with non-ASCII characters because (except within the distinguishedName) utf-8 is not used and Net::LDAP in version3 insists on writing utf-8.

      • either use Encode to iso-8859-1 and use Net::LDAP version2, e.g.
        my $ldap = Net::LDAP->new($hostname, version => 2) or die "Error: can't connect to '$hostname'";
      • or Win32::AD::User which is good for adding and editing users but doesn't support searches
      • or use pure Win32::OLE (that's the way I prefer). Argel postet some very good links below.

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Active Directory DC and OU
by GrandFather (Saint) on Apr 18, 2006 at 21:14 UTC

    Looked very carefully at your post, but I couldn't see the data that you were referring to anywhere. Is that because you haven't managed to grab it yet? Perhaps you could tell us something of the nature of this elusive data and what you want to do with it once you have managed to sprinkle salt on its tail? I really recommend that you read I know what I mean. Why don't you?!


    DWIM is Perl's answer to Gödel
      I think we can translate the OP question into "How can I query Active Directory using Perl".
Re: Active Directory DC and OU
by traveler (Parson) on Apr 18, 2006 at 21:42 UTC
    Each directory can have multiple DCs and OUs, so presumably you want that info for a particular entry or entries.

    I'm not running AD here so I can't test this. Here is an offsite link (sorry) to an article on developer.com that gives what appears to be a good example.

    Most perl programs for LDAP use Net::LDAP so look at the module, the examples that come with it as well as Net::LDAP:FAQ which, IMHO, is really good.

    HTH, --traveler