Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Answered powershell ldap query

  • Thursday, March 29, 2012 8:07 AM
     
      Has Code

    I got the following ps code:

    Write-Output "first_name,last_name,email_address,mailalias")
    
    $root = [ADSI]"LDAP://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    $search = [adsisearcher]$root
    
    $Search.Filter = "(&(objectCategory=person))"
    
    $colResults = $Search.FindAll()
    
    foreach ($i in $colResults)
    {
    $user = $i.GetDirectoryEntry()
    
    Write-Output "$($user.givenName),$($user.sn),$($user.mail),$($user.mailNickname)"
    }

    This works fine but the the mailnickname (alias) is empty i can see in in the ldap (adsiedit.msc) that the user got a mailnickname.

    anyone?

All Replies

  • Thursday, March 29, 2012 8:57 AM
     
      Has Code

    Hi,

    For me your script works fine.
    Maybe try a little different

    Write-Output "first_name,last_name,email_address,mailalias"
    
    $root = [ADSI]"LDAP://OU=xyz,DC=contoso,DC=com"
    $search = [adsisearcher]$root
    $Search.Filter = "(&(objectCategory=person))"
    $colResults = $Search.FindAll()
    
    foreach ($i in $colResults)
    {
     [string]$i.Properties.Item('givenName'),[string]$i.Properties.Item('sn'),[string]$i.Properties.Item('mail'),[string]$i.Properties.Item('mailNickname') -join ","
    }
    

  • Thursday, March 29, 2012 9:11 AM
     
      Has Code

    Its working fine for me with mailnick name.

    However, you can try an altrenative using Quest AD Commandlets which is very simple.

    Get-QADUser -SizeLimit 0 -SearchRoot "OU=OUNAME,DC=DOMAINNAME,DC=COM" -IP mailnickname |Select-Object Firstname, Lastname, mail, mailnickname

    Good luck !


    Shaba

  • Thursday, March 29, 2012 9:25 AM
     
     

    hai thx,

    i got now getting this:

    first_name last_name email_address mailalias
    John Besetmer jbese@xxx.co.uk System.Byte[]
  • Friday, March 30, 2012 7:24 AM
    Moderator
     
     Answered Has Code

    Hi,

    Please try the below code:

    Write-(Output "first_name,last_name,email_address,mailalias")
    
    get-aduser -filter * -properties *|select $_.givenname,$_.sn,$_.mail, $_.mailnickname
    

    In addition, the below link should be helpful:

    http://technet.microsoft.com/en-us/library/aa996205(v=exchg.65).aspx

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support


  • Friday, March 30, 2012 2:21 PM
     
     

    jachhorror, what code did you use to get the output you reported in your last reply? The column headings must have come from code we are not seeing. We don't know where "mailalias" came from, and it bothers me that the datatype for this field appears to be a byte array. This couldn't have resulted from the code you posted.


    Richard Mueller - MVP Directory Services