Solved

PERL - Search Active Directory for specific username

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

810 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