Link to home
Start Free TrialLog in
Avatar of makanzore
makanzore

asked on

Powershell find users DistinguishedName

Hello,
i need to find users DN with a csv liste of users name. So i have they's name and i want to find the DN. I have to do the LDAP root search becacause users are not on the same OU. This is my script but it dont work.

$ldapQuery=new-object directoryservices.directoryentry("LDAP://dc=mdl,dc=active")
$UserDetails=Import-Csv "C:\user1.csv"

$userdetails = $ldapQuery.psbase.Children

foreach($UD in $UserDetails)
{

$CN=$UD.CN
$accts=$ldapQuery.psbase.Children.Find("cn=$CN")
$accts.distinguishedName

}

my CSV file:

UserDetails
user1
user2
user3

Thank for your Help
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


Hey,

Searching will be required if you don't know where they are. There's  Name Translate as well, but it might just be easier to use the Directory Searcher.

You'd have an easier time of this with the Quest CmdLets:

http://www.quest.com/powershell/

It makes it much shorter:

Import-CSV "C:\user1.csv" | %{ Get-QADUser $_.CN | Select-Object DN }

Chris
Import-Csv "C:\user1.csv"  | %{
  $Searcher = New-Object `
    DirectoryServices.DirectorySearcher($Null, "(&(objectClass=user)(objectCategory=person)(cn=$($_.CN)))")
  $Searcher.FindAll() | Select-Object @{n='DistinguishedName';e={ $_.Properties["distinguishedname"] }}
}

Open in new window

Avatar of makanzore
makanzore

ASKER

Thanks for your answer
I download quest script to use the command get-QAduer but i got an error:

Get-QADUser : Impossible de valider l'argument sur le paramètre « Identity ». L'argument est null ou vide. Indiquez un
argument qui n'est pas null ou vide et réessayez.
Au niveau de ligne : 1 Caractère : 43
+ Import-CSV "C:\user1.csv" | %{ Get-QADUser <<<<  $_.CN | Select-Object DN }
    + CategoryInfo          : InvalidData: (:) [Get-QADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlet
   s.GetUserCmdlet

Thanks
And with the command
============================================
Import-Csv "C:\user1.csv"  | %{
  $Searcher = New-Object `
    DirectoryServices.DirectorySearcher($Null, "(&(objectClass=user)(objectCategory=person)(cn=$($_.CN)))")
  $Searcher.FindAll() | Select-Object @{n='DistinguishedName';e={ $_.Properties["distinguishedname"] }}
}
==========================================
I got this error:

Une erreur s'est produite lors de l'énumération parmi une collection : The (&(objectClass=user)(objectCategory=person)
cn=)) search filter is invalid..
Au niveau de ligne : 4 Caractère : 3
+    <<<< $Searcher.FindAll() | Select-Object @{n='DistinguishedName';e={ $_.Properties["distinguishedname"] }}
    + CategoryInfo          : InvalidOperation: (System.Director...sultsEnumerator:ResultsEnumerator) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : BadEnumeration

Can you show me what you have in that CSV file? Including the header line (if any).

Thanks,

Chris
This is the CSV file i use header line and users:

UserDetails
user1
user2
user3

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok it works
Excuse me i didn't understand

Thanks for all