perlquestion
OzzyOsbourne
<p>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.</p>
<p>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.</p>
<p>When the Non-working code is run, no records are returned whether the Page size property is set or not.</p>
<p>I waded through MS, google, and all the VB people I know (I didn't actually wade <i>through</i> the people, but you understand) and I'm still stuck. </p>
<readmore>
<H3>Working Code</H3>
<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);samAccountName,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();
}
}
</code>
<H3>Non Working Code</H3>
<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);samAccountName,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();
}
}
</code>
<p>-<a href="/index.pl?node=OzzyOsbourne&lastnode_id=1072">OzzyOsbourne</a></p>