Solved

PERL - Search Active Directory for specific username

Posted on 2011-09-28
2
453 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-_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
Here's a look at newsworthy articles and community happenings during the last month.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans
Suggested Courses

632 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