try the code below. it does an LDAP search using ADODB
 
regards.
 
$users = ldap2array_by_adsi_ado(
   "LDAP", #protocol name
   "mydc01", #DC name
   "DC=myregion,DC=mycountry,DC=mycom,DC=com", #base OU
   "(sAMAccountName=XXXX)", #filter string
   "cn,middleName,givenName,sn,displayName", #attributes collected
   1, #subtree?
   10 #pagesize
   );
if(scalar(@$users)=0){
 print "user exists";
}else{
 print "user does not exist";
}
 
sub ldap2array_by_adsi_ado{#returns reference to an array of objects found
 my ($protocol, $servername, $baseOU, $ldapFilter, $collectedAttributes, $subtree, $pagesize, $account, $password)[EMAIL PROTECTED];
 my ($objConnection, $objCommand);
 my ($strQuery, $rs, $arrayref, $i, $hashref);
 
 $objConnection = Win32::OLE->new("ADODB.Connection");
 
 #Open the Connection
 $objConnection->{Provider} = "ADsDSOObject";
 if($account){
  #logon explicitly if an account has been specified
  $objConnection->Properties("User ID")->{Value} = $account;
  $objConnection->Properties("Password")->{Value} = $password;
 }
 
 $objConnection->Open("ADs Provider");
 
 $objCommand = Win32::OLE->new("ADODB.Command");
 $objCommand->{ActiveConnection} = $objConnection;
 
 $strQuery = "<$protocol://$servername/$baseOU>;$ldapFilter;$collectedAttributes";
 if($subtree){
  $strQuery .= ";subtree";
 }
 #print "$strQuery\n";
 $objCommand->{CommandText} = $strQuery;
 #page size should be big enough to hold all results
 $objCommand->Properties("Page size")->{Value} = $pagesize;
 
 #run the LDAP search
 $rs = $objCommand->Execute();
 
 $arrayref=[];
 
 while(!$rs->{EOF}){
  #write each LDAP entity to file
  $hashref={};
  for($i=0; $i<$rs->Fields->{Count}; $i++){
   $hashref->{$rs->Fields($i)->{Name}} = $rs->Fields($i)->{Value};
  }
  push @$arrayref, $hashref;
  $rs->MoveNext();
 }
 $rs->Close();
 return $arrayref;
}
 
-----Original Message-----
From: David Nickel [mailto:[EMAIL PROTECTED]
Sent: Friday, March 26, 2004 10:27 AM
To: [EMAIL PROTECTED]
Subject: Checking if a user exist

I am trying to write a little subroutine that will take a username search and then search an Organizational Unit on a Windows 2003 AD Domain and return if the user exist or if he does not.

I tried the following snippet guessing that the Create call would fail if the user already exists, but that does not seem to be the case. Can any of you guy's tell me or guide me in the direction where I can find an efficient way to do this. Any help would be much appreciated.

Thank you,

David

 

use Win32::OLE;

my $ADsPath = "LDAP://ou=ouname,dc=my,dc=domain";

my $connect = Win32::OLE-> GetObject($ADsPath);

my $username = 'usernamel';

 

if (my $user = $connect->Create("User", "$username"){

print "User does not exist!!\n";

}else{

  print "User exists!!\n";

----- This message is for the named person's use only. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please delete it and all copies from your system, destroy any hard copies and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. Nomura Holding America Inc., Nomura Securities International, Inc, and their respective subsidiaries each reserve the right to monitor all e-mail communications through its networks. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state the views of such entity. Unless otherwise stated, any pricing information in this message is indicative only, is subject to change and does not constitute an offer to deal at any price quoted. Any reference to the terms of executed transactions should be treated as preliminary only and subject to our formal written confirmation.

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to