<?xml version="1.0" encoding="windows-1252"?>
<node id="196718" title="ADSI: Getting a full list of users from a w2k domain" created="2002-09-10 11:34:41" updated="2005-06-14 01:21:40">
<type id="115">
perlquestion</type>
<author id="21786">
OzzyOsbourne</author>
<data>
<field name="doctext">
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;When the Non-working code is run, no records are returned whether the Page size property is set or not.&lt;/p&gt;
&lt;p&gt;I waded through MS, google, and all the VB people I know (I didn't actually wade &lt;i&gt;through&lt;/i&gt; the people, but you understand) and I'm still stuck.  &lt;/p&gt;
&lt;readmore&gt;
&lt;H3&gt;Working Code&lt;/H3&gt;
&lt;code&gt;use strict;
use Win32::OLE 'in';

my $out="accounts.tmp";
open OUT,"&gt;$out" or die "Can't open $out for write";
&amp;get_corp_accts();
close OUT;

sub get_corp_accts{
	
	# get ADO object, set the provider, open the connection
	my $ADO = Win32::OLE-&gt;new("ADODB.Connection");
	$ADO-&gt;{Provider} = "ADsDSOObject";
	$ADO-&gt;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-&gt;Execute("&lt;$ADSPath&gt;;(objectClass=User);samAccountName,HomeDirectory;SubTree");
	
	until ($users-&gt;EOF){
		my $homeDir=lc($users-&gt;Fields(1)-&gt;{Value});
    	if ($homeDir=~/^\\\\mf/){
			my $account=lc($users-&gt;Fields(0)-&gt;{Value});
			print OUT "$account\t$homeDir\n";
		}
    	$users-&gt;MoveNext;
	}
	$users-&gt;Close;
	$ADO-&gt;Close;
	print "Wrote Accounts\n";
}

sub ole_error_check{
	if (Win32::OLE-&gt;LastError(  )){
		die Win32::OLE-&gt;LastError();
	}
}
&lt;/code&gt;
&lt;H3&gt;Non Working Code&lt;/H3&gt;
&lt;code&gt;
use strict;
use Win32::OLE 'in';

my $out="accounts.tmp";
open OUT,"&gt;$out" or die "Can't open $out for write";
&amp;get_corp_accts();
close OUT;

sub get_corp_accts{
	
	# get ADO object, set the provider, open the connection
	my $ADO = Win32::OLE-&gt;new("ADODB.Connection");
	my $ADOCmd=Win32::OLE-&gt;new("ADODB.Command"); #new
	$ADO-&gt;{Provider} = "ADsDSOObject";
	$ADO-&gt;Open("ADSI Provider");
	$ADOCmd-&gt;Properties-&gt;{'Page Size'}=10000;#new
	my $ADSPath = "LDAP://OU=Users,OU=group,DC=subdomain,DC=domain,DC=com";
	$ADOCmd-&gt;{ActiveConnection}=$ADO;#new
	$ADOCmd-&gt;{CommandText}="&lt;$ADSPath&gt;;(objectClass=User);samAccountName,HomeDirectory;SubTree";#new
	# prepare and then execute the query	
	my $users=$ADOCmd-&gt;Execute;
	
	until ($users-&gt;EOF){
		my $homeDir=lc($users-&gt;Fields(1)-&gt;{Value});
    	if ($homeDir=~/^\\\\mf/){
			my $account=lc($users-&gt;Fields(0)-&gt;{Value});
			print OUT "$account\t$homeDir\n";
		}
    	$users-&gt;MoveNext;
	}
	$users-&gt;Close;
	$ADO-&gt;Close;
	print "Wrote Accounts\n";
}

sub ole_error_check{
	if (Win32::OLE-&gt;LastError(  )){
		die Win32::OLE-&gt;LastError();
	}
}
&lt;/code&gt;
&lt;p&gt;-&lt;a href="/index.pl?node=OzzyOsbourne&amp;lastnode_id=1072"&gt;OzzyOsbourne&lt;/a&gt;&lt;/p&gt;</field>
</data>
</node>
