Active Directory Lookup

Author Message
marcussmith

  • Total Posts : 7
  • Scores: 0
  • Reward points : 0
  • Joined: 1/30/2006
  • Status: offline
Active Directory Lookup Wednesday, December 05, 2007 2:43 AM (permalink)
0
Hi all,

Im trying to do a active directory lookup using powershell to find the users email address from the sAMAccountName's listed in a array or text file.  At the moment I have got this far but not sure if its the best way and this way gets me more than just the emailaddress@domain.com

###########################################################

cls

$SAMName = "users_sam_id"

  $Searcher = New-Object DirectoryServices.DirectorySearcher
  $Searcher.Searchroot ="LDAP://OU=XXXXXX,OU=XXXXX),DC=XXXXX,DC=XXX"
  $searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
                                                                                                                   
  $userlist = "mail"
  $userlist | foreach {[void]$Searcher.Propertiestoload.Add($_)}
 
  $userlist= $searcher.findall()
 
  $results = $searcher.findall()
   
foreach ($result in $results){$result.properties}

$result.properties |ft value | out-file searchemail.txt

#############################################################

This works except it gives me more than just the emailaddress@domain.com and doesnt read the sAMAccountName's from a text file.

 Any help on this would be great.

 
#1
    SAPIENScripter

    • Total Posts : 283
    • Scores: 2
    • Reward points : 0
    • Joined: 11/1/2006
    • Location: SAPIEN Technologies
    • Status: offline
    RE: Active Directory Lookup Wednesday, December 05, 2007 4:07 AM (permalink)
    0
    Here's one way you could handle this:

    $Searcher = New-Object DirectoryServices.DirectorySearcher
    $Searcher.Searchroot ="LDAP://DC=company,DC=local"
    get-content sams.txt | % {
      write $_
      $searcher.filter = "(&(objectClass=user)(sAMAccountName= $_))"
      $searcher.FindOne().Properties.mail
    }

    Because you are filtering on the Samaccountname you should only get one match so you only need to find the first match.  If you can use the free Quest AD cmdlets this can be done even easier:

    cat sams.txt | % {get-qaduser $_ | select dn,mail}


    Jeffery Hicks
    Windows PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

    Follow Me: http://www.twitter.com/JeffHicks
     
    #2
      marcussmith

      • Total Posts : 7
      • Scores: 0
      • Reward points : 0
      • Joined: 1/30/2006
      • Status: offline
      RE: Active Directory Lookup Wednesday, December 05, 2007 8:53 PM (permalink)
      0
      Thanks for your reply that was very useful,
       
      I am now trying to get the results output to a varible to use later when sending a email in the script, at the moment I can output the results to a text file by doing
       
      $searcher.FindOne().Properties.mail | Out-File -filepath "C:\ADScripts\AD Lookup\lookupresult.txt"  -append
       
      The problem is this wont work if I change it to out-string or outvariable.  Also without using the -append on the text file it only stores the email address of the sam last checked.
       
      Hope this makes sense.
       
      Regards
      Marcus
       
      #3
        SAPIENScripter

        • Total Posts : 283
        • Scores: 2
        • Reward points : 0
        • Joined: 11/1/2006
        • Location: SAPIEN Technologies
        • Status: offline
        RE: Active Directory Lookup Wednesday, December 05, 2007 11:12 PM (permalink)
        0
        Save your results to a variable, then you can use the variable to send output to a file and do whatever else you want.


        $Searcher = New-Object DirectoryServices.DirectorySearcher
        $Searcher.Searchroot ="LDAP://DC=company,DC=local"
        get-content sams.txt | % {
        write $_
        $searcher.filter = "(&(objectClass=user)(sAMAccountName= $_))"
        $results+=$searcher.FindOne().Properties.mail
        }

        The variable $results can now be piped to Out-file and you can use it for other things as well.
        Jeffery Hicks
        Windows PowerShell MVP
        SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

        Follow Me: http://www.twitter.com/JeffHicks
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9