Solved

PERL - Search Active Directory for specific username

Posted on 2011-09-28
2
428 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

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Synchronize a new Active Directory domain with an existing Office 365 tenant
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

20 Experts available now in Live!

Get 1:1 Help Now