stringpowershellactive-directoryldapquest

How to avoid Get-QADUser queries to return multiple property strings with PowerShell 2.0


I am using Get-QADUser and .DirectoryEntry.FirstName in PowerShell 2.0 to make multiple LDAP queries and return strings. These multiple calls are slow. How could I make one Get-QADUser LDAP query only and create strings from select properties.

Example:

    ADD-PSSnapin Quest.Activeroles.ADManagement
    $searchDomainFrom = 'domain.net/OUPath/Users'
    $user = UserName

    $FirstName = (Get-QADUser -SearchRoot $searchDomainFrom $user).DirectoryEntry.FirstName
    $LastName = (Get-QADUser -SearchRoot $searchDomainFrom $user).DirectoryEntry.LastName
    $Email = (Get-QADUser -SearchRoot $searchDomainFrom $user).DirectoryEntry.Email
    $Supervisor = (Get-QADUser -SearchRoot $searchDomainFrom $user).DirectoryEntry.Manager
    $OfficeLocation = (Get-QADUser -SearchRoot $searchDomainFrom $user).DirectoryEntry.Office
    $PhoneNumber = (Get-QADUser -SearchRoot $searchDomainFrom $user).DirectoryEntry.PhoneNumber

Solution

  • Just make the query once and then gets your strings from the object that's returned the first time.

    $Current = Get-QADUser -SearchRoot $searchDomainFrom $user
    $FirstName = $Current.DirectoryEntry.FirstName
    $LastName = $Current.DirectoryEntry.LastName
    $Email = $Current.DirectoryEntry.Email
    $Supervisor = $Current.DirectoryEntry.Manager
    $OfficeLocation = $Current.DirectoryEntry.Office
    $PhoneNumber = $Current.DirectoryEntry.PhoneNumber