Solved

Find User Home Directory LDAP

Posted on 2008-10-08
4
2,479 Views
Last Modified: 2013-12-24
I am trying (rather unsuccessfully) to retrieve a user's home directory path from Active Directory. Users login to my system using their network username; I need to take this username value and retrieve the user's home directory path for it. The code below works, but requires that I know the user's full and last name, and OU path - something which will obviously change.

I can't seem to find any information on how to query Active Directory based on user username, only full name. Could somebody help?
On Error Resume Next
Set objUser = GetObject _
  ("LDAP://CN=John Doe,OU=Test Users,OU=User Accounts,OU=The Company,DC=domain,DC=internal")
objUser.GetInfo
 
strProfilePath = objUser.Get("profilePath")
strScriptPath = objUser.Get("scriptPath")
strHomeDirectory = objUser.Get("homeDirectory")
strHomeDrive = objUser.Get("homeDrive")
 
WScript.echo "profilePath: " & strProfilePath
WScript.echo "scriptPath: " & strScriptPath
WScript.echo "homeDirectory: " & strHomeDirectory
WScript.echo "homeDrive: " & strHomeDrive

Open in new window

0
Comment
Question by:tigermatt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
JohnGerhardt earned 500 total points
ID: 22670967
This is what we use...
Function SearchUser(strUser)
	Const ADS_SCOPE_SUBTREE = 2
	Const ADS_UF_ACCOUNTDISABLE = 2 
 
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand = CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"
	Set objCommand.ActiveConnection = objConnection
 
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
'ENTER DOMAIN DETAILS HERE
	objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://dc=DOMAIN,dc=internal' WHERE objectCategory='user' " & "AND name=' " & strUser & "'"
	
	Set objRecordSet = objCommand.Execute
	
	
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			WScript.Echo "User found in AD!"
			Success = True
				
			UserVar = objRecordSet.Fields("ADsPath").Value
			Dim objUSer
			Set objUSer = GetObject(Uservar)
			
			
			SamAccountName = objUser.samAccountName 
				Cn = objUser.CN
				DisplayName = objUser.DisplayName
				FirstName = objUser.GivenName 
				LastName = objUser.sn 
				initials = objUser.initials 
				Descrip = objUser.description 
			
				Profile = objUser.profilePath 
				LoginScript = objUser.scriptpath 
				HomeDirectory = objUser.HomeDirectory 
				HomeDrive = objUser.homeDrive 
				AdsPath = objUser.Adspath 
				'LastLogin = objUser.LastLogin
				
				WScript.Echo vbTab & "SAM Account Name:" & vbTab & SamAccountName
				WScript.Echo vbTab & "CN:" & vbTab &  vbTab & vbTab & vbTab & vbTab & Cn
				WScript.Echo vbTab & "Display Name:" & vbTab &  vbTab & DisplayName
				WScript.Echo vbTab & "First Name:" & vbTab &  vbTab & vbTab & FirstName
				WScript.Echo vbTab & "Last Name:" & vbTab &  vbTab & vbTab & LastName
				WScript.Echo vbTab & "Initials:" & vbTab &  vbTab & vbTab & initials
				WScript.Echo vbTab & "Description:" & vbTab &  vbTab & Descrip
				WScript.Echo vbTab & "Profile:" & vbTab & vbTab & vbTab & Profile
				WScript.Echo vbTab & "LoginScript:" & vbTab &  vbTab & vbTab & LoginScript
				WScript.Echo vbTab & "HomeDirectory:" & vbTab & vbTab & HomeDirectory
				WScript.Echo vbTab & "HomeDrive:" & vbTab & vbTab & vbTab & HomeDrive
				WScript.Echo vbTab & "AdsPath:" & vbTab & vbTab & vbTab & AdsPath
				WScript.Echo vbTab & "LastLogin:" & vbTab & vbTab & vbTab & LastLogin 
			
			
			If objUser.AccountDisabled = False Then
				WScript.Echo "User Account found Enabled"
				'objUser.Put "UserFlags", Flags Or ADS_UF_ACCOUNTDISABLE
				objUser.AccountDisabled = True
				objUser.SetInfo
				
				WScript.Echo "User Account Enabled?" & objUser.AccountDisabled
			End If
			If objUser.AccountDisabled = True Then
				WScript.Echo "User Account found Disabled"
				'objUser.Put "UserFlags", Flags Or ADS_UF_ACCOUNTDISABLE
				objUser.AccountDisabled = False
				objUser.SetInfo
				WScript.Echo "User Account Enabled?" & objUser.AccountDisabled
			end If
			objRecordSet.MoveNext
		Loop
	
	
End Function

Open in new window

0
 
LVL 58

Author Comment

by:tigermatt
ID: 22671314
Thanks for that. I did manage to condense it down a bit - rather than do an SQL-type query to get the full LDAP path to the user, then go and look that path up, I managed to use your code and pull the homeDirectory variable directly from Active Directory in the SQL lookup.

Thanks very much for the pointer :)
Const ADS_SCOPE_SUBTREE = 2
 
'Be sure to set the username variable to the name of the username to lookup
'elsewhere in your script (this does not automatically retrieve it!)
 
Dim objConnection: Set objConnection = CreateObject("ADODB.Connection")
Dim objCommand: Set objCommand = CreateObject("ADODB.Command")
 
Dim objRootDSE: Set objRootDSE = GetObject("LDAP://rootDSE")
Dim strDomain: strDomain = objRootDSE.Get("defaultNamingContext")
Set objRootDSE = Nothing
 
With objConnection
	.Provider = "ADsDSOObject"
	.Open "Active Directory Provider"
End With
 
Set objCommand.ActiveConnection = objConnection
 
With objCommand
	.Properties("Page Size") = 1000
	.Properties("SearchScope") = ADS_SCOPE_SUBTREE
	.CommandText = "SELECT homeDirectory FROM 'LDAP://" & strDomain & "' WHERE objectCategory = 'user' AND sAMAccountName = '" & username & "'"
End With
 
Dim rsUser: Set rsUser = objCommand.Execute
 
Dim strHomeDir: strHomeDir = rsUser.fields("homeDirectory").value
 
If Left(strHomeDir, 2) = "\\" Then
	
	Dim strHomeDirR: strHomeDirR = Right(strHomeDir, Len(strHomeDir) - 2)
	Dim homeServer: homeServer = Mid(strHomeDirR,1,Instr(strHomeDirR, "\")-1)
 
End If
 
Set strDomain = Nothing
Set strHomeDir = Nothing
Set strHomeDirR = Nothing
Set homeServer = Nothing
rsUser.Close
objConnection.Close
Set rsUser = Nothing
Set objCommand = Nothing
Set objConnection = Nothing

Open in new window

0
 
LVL 58

Author Closing Comment

by:tigermatt
ID: 31504333
Thanks! You may be interested in my comment I just posted as to how I eliminated a few lines of code... :)
0
 
LVL 17

Expert Comment

by:JohnGerhardt
ID: 22671752
That is definetly a bit neater..  
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
google retention policy 4 124
Need to trim my database size 9 52
What is needed to become a DBA? 7 50
SQL Server Reports (SSRS 2014) - Reports Timing Out 11 111
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question