Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PERL - Search Active Directory for specific username

Posted on 2011-09-28
2
Medium Priority
?
461 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

715 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