根据我阅读的所有文档,查询 Active Directory 时结果集的最大大小为 1000 条记录。如果您需要超过 1000 条记录,系统会告诉您使用 Net::LDAP::Control::Paged 来分页并获取超过 1000 条记录,我曾尝试这样做。出于某种原因,尽管使用了分页控件,但我仍然只能返回 1000 条记录。我使用的代码与其他人声称成功使用的代码相同,但没有任何运气。我在这里错过了什么吗?请注意,我没有对 AD 服务器的管理访问权限,因此更改配置不是我的选择。我在下面粘贴了相关代码,因此您可以指出我可能做错了什么。
#!perl -w
use strict;
use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
my $LDAPServer = "<LDAP SERVER ADDRESS>";
my $base = "<BASE DN>";
my $user = "<LDAP USER>";
my $pass = "<PASSWORD>";
my $page = Net::LDAP::Control::Paged->new(size => 1000) or die $!;
my $errorConnectionStr = "";
my $ldap;
my $mesg;
my $cookie;
$ldap = Net::LDAP->new($LDAPServer);
$mesg = $ldap->bind($user, password => $pass, version => 3);
open(OUT,">users.csv");
print OUT "sAMAccountName,sn,givenName\n";
my $filter = "(&(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(!(objectClass=computer)))";
my @attribs = ("sn", "givenName", "sAMAccountName", "distinguishedName");
my @args = (
base => $base,
scope => 'sub',
filter => $filter,
attrs => @attribs,
pagesize => 1000,
control => [$page]
);
while(1) {
$mesg = $ldap->search(@args) or die $!;
while (my $entry = $mesg->pop_entry()) {
my $print = 1;
my $dn = $entry->dn();
my $uid = $entry->get_value('sAMAccountName');
my $ln = $entry->get_value('sn');
my $fn = $entry->get_value('givenName');
if($dn =~ m/.*Resources.*/) {
$print = 0;
}
if(defined($uid) && defined($ln) && defined($fn)) {
if($print == 1) {
print OUT "$uid,$ln,$fn\n";
}
}
}
my ($resp) = $mesg->control(LDAP_CONTROL_PAGED) or last;
$cookie = $resp->cookie or last;
$page->cookie($cookie);
}
if($cookie) {
print "Abnormal exit\n";
$page->cookie($cookie);
$page->size(0);
$ldap->search(@args);
}
close(OUT);
$ldap->unbind;