There may be better ways to do this, but I simply use Win32::OLE to traverse the LDAP hierarchy. This is quick code, but it should point you in the right direction. This will print everything, including a lot of things you probably don't care to see. You could decide whether to look at an object based on its class, and also if it was a class like "user", print out some additional attributes.
use Win32::OLE qw(in);
showObjects("LDAP://[INSERT DOMAIN NAME HERE]","");
sub showObjects {
my ($path, $space) = @_;
my $object = Win32::OLE->GetObject($path);
print "$space $object->{Name} ($object->{Class})\n";
foreach my $childObject (in $object) {
showObjects($childObject->ADsPath, $space . " ");
}
}
I can't take credit for this approach—I basically translated a solution in
O'Reilly's Active Directory Cookbook from VBScript to Perl. If you have to do a lot of AD scripting, this book would probably be a good investment. It's pretty simple to translate its VBScript code into Perl.