| 
| 
| 
| #187404 - 2008-04-30 09:05 PM  Searching for a specific user in AD via the SAM account name |  
| Thallium   Just in Town
 
 Registered:  2002-02-26
 Posts: 4
 | 
I need to search for a user based on their SAM account name; I've tried different variations on GetObject("LDAP://etc") and while I can connect to Active Directory, query the OU I want and list everything in it as an array, I need to be more specific and only search for the exact object I need. How can I do this?
 |  
| Top |  |  |  |  
| 
| 
| #187427 - 2008-05-01 03:45 PM  Re: Searching for a specific user in AD via the SAM account name
[Re:  Thallium] |  
| Benny69   Moderator
 
       
   Registered:  2003-10-29
 Posts: 1036
 Loc:  Lincoln, Ne
 | 
Thallium,Is this what you are looking for:
 
 
CheckForUser("tuser") ;samAccountName
Function CheckForUser($samAccountName)
	
  $objRootDSE = GetObject("LDAP://rootDSE")
  $defaultNamingContext = $objRootDSE.Get("defaultNamingContext")
  
	$strUserName = $samAccountName
	$objConnection = CreateObject("ADODB.Connection")
	$objConnection.Open ("Provider=ADsDSOObject;")
	$objCommand = CreateObject("ADODB.Command")
	$objCommand.ActiveConnection = $objConnection
	$objCommand.CommandText = "<LDAP://" + $defaultNamingContext + ">;(&(objectCategory=User)(samAccountName=" + $strUserName + "));samAccountName;subtree"
   
	$objRecordSet = $objCommand.Execute
	If $objRecordset.RecordCount = 0
		? "sAMAccountName: " + $strUserName + " does not exist."
	Else
		? $strUserName + " exists."
	EndIf
	$objConnection.Close
EndFunction
 |  
| Top |  |  |  |  
| 
| 
| #187431 - 2008-05-02 12:42 AM  Re: Searching for a specific user in AD via the SAM account name
[Re:  Benny69] |  
| Thallium   Just in Town
 
 Registered:  2002-02-26
 Posts: 4
 | 
Benny69 - that works great, thanks!
 |  
| Top |  |  |  |  
| 
| 
| #189272 - 2008-08-26 01:23 AM  Re: Searching for a specific user in AD via the SAM account name
[Re:  Benny69] |  
| Gerald_G   Fresh Scripter
 
 Registered:  2005-04-11
 Posts: 34
 Loc:  Canada
 | 
How would you modify this to return the users CN once found ?
 |  
| Top |  |  |  |  
| 
| 
| #189286 - 2008-08-26 06:52 PM  Re: Searching for a specific user in AD via the SAM account name
[Re:  Gargoyle] |  
| Gerald_G   Fresh Scripter
 
 Registered:  2005-04-11
 Posts: 34
 Loc:  Canada
 | 
I can't get translatename() to find a user at all.Not sure where I'm going wrong.
 
 I also notice in the notes, no Integer value for NameType for samAccountNAme. (which is what I need to find the user by).
 
 |  
| Top |  |  |  |  
| 
| 
| #189288 - 2008-08-26 08:09 PM  Re: Searching for a specific user in AD via the SAM account name
[Re:  Gerald_G] |  
| Gerald_G   Fresh Scripter
 
 Registered:  2005-04-11
 Posts: 34
 Loc:  Canada
 | 
Edit - I got it thanks.
 Figured out how to add fields to the recordset, and then access them using RecordSet.fields().
 
 I now have a function that looks up an ID by samAccountName and returns the CN.
 
 Thanks again folks !!
 
 |  
| Top |  |  |  |  
 Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
 
 | 
| 
 
| 0 registered
and 739 anonymous users online. 
 | 
 |  |