Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Active Directory Lookup

 
Logged in as: Guest
arrSession:exec spGetSession 2,5,54034
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Windows PowerShell >> Active Directory Lookup
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Active Directory Lookup - 12/5/2007 1:43:08 AM   
  marcussmith

 

Posts: 5
Score: 0
Joined: 1/30/2006
Status: offline
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.
 
 
Post #: 1
 
 RE: Active Directory Lookup - 12/5/2007 3:07:14 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
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

(in reply to marcussmith)
 
 
Post #: 2
 
 RE: Active Directory Lookup - 12/5/2007 7:53:35 PM   
  marcussmith

 

Posts: 5
Score: 0
Joined: 1/30/2006
Status: offline
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

(in reply to SAPIENScripter)
 
 
Post #: 3
 
 RE: Active Directory Lookup - 12/5/2007 10:12:01 PM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
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

(in reply to marcussmith)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Windows PowerShell >> Active Directory Lookup Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts