Search LDAP Entries with ldapsearch on Linux/Unix

 Uncategorized  Kommentare deaktiviert für Search LDAP Entries with ldapsearch on Linux/Unix
Dez 092016
 

RedHat ldapsearch Documentation

ldapsearch -D "CN=<MyUser>,OU=S,OU=Useraccounts,DC=de,DC=<COMPANY>,DC=com" -w "PASSWORD" -b "DC=<MyCompany>,DC=com" 
   -s sub "(& (objectClass=user) (name=TestUser1))" -h "myLdapHost.de.myCompany.com" -p "3268" telephonenumber name department


# Result:
# extended LDIF
#
# LDAPv3
# base <DC=<MyCompany>,DC=com> with scope subtree
# filter: (& (objectClass=user) (name=TestUser1))
# requesting: telephonenumber name department
#

# TestUser, M, Useraccounts, XX, emea.MyCompany.com
dn: CN=TestUser1,OU=M,OU=Useraccounts,OU=XX,DC=emea,DC=MyCompany,DC=com
department: <HIDDEN>
name: TestUser1
telephonenumber : 0711-XXXXXXXXX

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Description:
-D     Username (BaseDN) that is used to connect to ldap
-w     Password to connect to ldap
-b     Specifies the starting point for the search
-s     Specifies the scope of the search
sub  search also in subtree
(& (objectClass=user) (name=mcp8wz)) = filter for objects with objectClass=user and where attribute „name“ = mcp8wz
-h     hostname of LDAP Server
-p     Port of LDAP Server
at the end of the command you can add all attributes you would like to fetch from LDAP. Leave empty to retrieve all information

 

Search LDAP Entries using Powershell

 IT  Kommentare deaktiviert für Search LDAP Entries using Powershell
Dez 092016
 

Below little script shows how to connect to Active Directory / LDAP and search for entries.

Docs: https://technet.microsoft.com/en-us/library/ff730967.aspx

$mySearcher = New-Object System.DirectoryServices.DirectorySearcher

# it is possible to specify manually a ldap search Path:
#$mySearcher.SearchRoot = "LDAP://DC=de,DC=myComapny,DC=com"

# or to get current ldap path (for example: DC=de,DC=myComapny,DC=com)
$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$mySearcher.SearchRoot = $objDomain

# search for object class "user" with attribte "name=TestUser1"
$mySearcher.Filter = "(& (objectClass=user) (name=TestUser1))"
$mySearcher.SearchScope = "sub"
$mySearcher.PageSize = 10

# specifiy the attributes you would like to retrieve
$myAttributes = ("telephonenumber", "mail", "department")
# comment below line to get all attributes
$mySearcher.PropertiesToLoad.AddRange($myAttributes)

$abc = $mySearcher.FindAll()

# show all attributes and values
foreach ($i in $abc.Properties.PropertyNames){
    Write-Host $i , "=" , $abc.Properties.$i
}

The Script will generate the following output:

mail = Testuser1@mycompany.com
telephonenumber = +49XXXXXXXX
department = MyDepartment
adspath = LDAP://<removed_by_author>