I am trying to return the users and their home directories from a win2k domain to a text file. I can get the first piece of exmple code to work. Unfortunately, ADSI will only return 1000 records.
To get around this limitation, VB examples seem to create a command object, set the Page Size property, and execute it. There are endless VB Examples (of course!), but I'm finding it difficult to find the right syntax in Perl.
When the Non-working code is run, no records are returned whether the Page size property is set or not.
I waded through MS, google, and all the VB people I know (I didn't actually wade through the people, but you understand) and I'm still stuck.
Working Code
use strict;
use Win32::OLE 'in';
my $out="accounts.tmp";
open OUT,">$out" or die "Can't open $out for write";
&get_corp_accts();
close OUT;
sub get_corp_accts{
# get ADO object, set the provider, open the connection
my $ADO = Win32::OLE->new("ADODB.Connection");
$ADO->{Provider} = "ADsDSOObject";
$ADO->Open("ADSI Provider");
my $ADSPath = "LDAP://OU=Users,OU=group,DC=subdomain,DC=domain,DC=
+com";
# prepare and then execute the query
my $users = $ADO->Execute("<$ADSPath>;(objectClass=User);samAccoun
+tName,HomeDirectory;SubTree");
until ($users->EOF){
my $homeDir=lc($users->Fields(1)->{Value});
if ($homeDir=~/^\\\\mf/){
my $account=lc($users->Fields(0)->{Value});
print OUT "$account\t$homeDir\n";
}
$users->MoveNext;
}
$users->Close;
$ADO->Close;
print "Wrote Accounts\n";
}
sub ole_error_check{
if (Win32::OLE->LastError( )){
die Win32::OLE->LastError();
}
}
Non Working Code
use strict;
use Win32::OLE 'in';
my $out="accounts.tmp";
open OUT,">$out" or die "Can't open $out for write";
&get_corp_accts();
close OUT;
sub get_corp_accts{
# get ADO object, set the provider, open the connection
my $ADO = Win32::OLE->new("ADODB.Connection");
my $ADOCmd=Win32::OLE->new("ADODB.Command"); #new
$ADO->{Provider} = "ADsDSOObject";
$ADO->Open("ADSI Provider");
$ADOCmd->Properties->{'Page Size'}=10000;#new
my $ADSPath = "LDAP://OU=Users,OU=group,DC=subdomain,DC=domain,DC=
+com";
$ADOCmd->{ActiveConnection}=$ADO;#new
$ADOCmd->{CommandText}="<$ADSPath>;(objectClass=User);samAccountNa
+me,HomeDirectory;SubTree";#new
# prepare and then execute the query
my $users=$ADOCmd->Execute;
until ($users->EOF){
my $homeDir=lc($users->Fields(1)->{Value});
if ($homeDir=~/^\\\\mf/){
my $account=lc($users->Fields(0)->{Value});
print OUT "$account\t$homeDir\n";
}
$users->MoveNext;
}
$users->Close;
$ADO->Close;
print "Wrote Accounts\n";
}
sub ole_error_check{
if (Win32::OLE->LastError( )){
die Win32::OLE->LastError();
}
}
-OzzyOsbourne