none
powershell ldap query

    Question

  • 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?

    Thursday, March 29, 2012 8:07 AM

Answers

All replies

  • 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 8:57 AM
  • 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:11 AM
  • hai thx,

    i got now getting this:

    first_name last_name email_address mailalias
    John Besetmer jbese@xxx.co.uk System.Byte[]
    Thursday, March 29, 2012 9:25 AM
  • 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 7:24 AM
  • 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

    Friday, March 30, 2012 2:21 PM
  • I stumbled upon this post because I notice the same strange behavior.

    The relevant part of my script:

        $Searcher = [ADSISEARCHER]($baseOU)
        $Searcher.SearchScope = "OneLevel"
        $Searcher.PropertiesToLoad.Add("mailnickname") | Out-Null
        $Searcher.Filter = "(&(objectClass=user)(!(sSCextensionAttribute006=*)))"
        $MBoxes = $Searcher.Findall()
        foreach ($MBox in $MBoxes) {
            Write-Host "  Cleaning mailbox [$($Mbox.Properties['mailnickname'][0])]"
        }

    Produces this output:

    [DBG]: PS C:\Data\projects>> 
      Cleaning mailbox [70 67 69 53 57 57]
      Cleaning mailbox [69 67 70 53 48 56]
      Cleaning mailbox [65 73 67 53 50 57]
      Cleaning mailbox [65 72 68 53 53 57]

    However, on the same machine, this code:

    $Domain = [ADSI]"LDAP://melinda.local"
    $Searcher = [ADSISearcher]$Domain
    $Searcher.Filter = "(&(objectClass=*)(mailnickname=FCE599))"
    $Searcher.PropertiesToLoad.Add("mailnickname") | Out-Null
    $recipient = $Searcher.FindOne()
    $recipient.Properties["mailnickname"][0]
    $recipient.Properties["mailnickname"][0].gettype()
    $host.Version

    produces

    PS C:\Data\projects> C:\temp\MAM\MailNicknameIssues.ps1
    FCE599
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                           
    -------- -------- ----                                     --------                                                                                                                           
    True     True     String                                   System.Object                                                                                                                      
    
    Major         : 3
    Minor         : 0
    Build         : -1
    Revision      : -1
    MajorRevision : -1
    MinorRevision : -1
    Try to explain this one.

    Tuesday, October 22, 2013 8:44 AM