Solved

PERL - Search Active Directory for specific username

Posted on 2011-09-28
2
431 Views
Last Modified: 2012-05-12
I am trying to write a script in perl to search active directory for a certain username.

I wrote it in powershell, as an example:
$first = "Tom"
$last = "Themachine"
$firstchar = $first.substring(0,1)
echo "$last$firstchar"
$account1 = "$last$firstchar"
$struser = "$account1"
 $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
 $search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(sAMAccountName=$struser)"
$result = $search.FindAll()
 if ($result -ne $null){Write-Host $struser " Exists"}
else {
Write-Host $struser "does not exist" }

Open in new window

This returns "ThemachineT does not exist" ..because the username ThemachineT does not exists in my domain
I am looking for good script examples, not one word hints. I need good examples I can work with. Thanks! I will reward maximum points!
0
Comment
Question by:_-W-_
2 Comments
 
LVL 74

Expert Comment

by:Glen Knight
ID: 36814931
What provider are you using?

I am by no means a scripting or programming guru but it doesn't look like you have specified one?

See here: http://support.microsoft.com/kb/187529

and an example script here: http://www.roth.net/blog/index.php/2006/04/27/how-do-you-query-active-directory-with-perl/
0
 
LVL 10

Accepted Solution

by:
jeromee earned 500 total points
ID: 36815569
Attached is an example that works for me...
use strict;
use Net::LDAP;

my $LDAP_HOST = "sd-dc02-la.winssi.itgssi.com";
my $LDAP_BASE = "DC=com";
my $search = $ARGV[0] || '(objectclass=*)'; # Here's an example of a search (sAMAccountName=jerome)                                                 

my $ldap = Net::LDAP->new($LDAP_HOST, port => 3268) or die "$@";
my $mesg = $ldap->bind(
        "cn=someuser,cn=users,dc=aaaaaa,dc=bbbbb,dc=com",       # login
        password => "xxxxxx"                                    # password                                                                          
        );

$mesg->code && die $mesg->error;
$mesg = $ldap->search (base   => $LDAP_BASE,
                       scope  => 'sub',
                       filter => $search,
                       );
$mesg->code && die $mesg->error;
foreach my $entry ($mesg->all_entries) { 
   $entry->dump;
}

$ldap->unbind;   # take down session

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now