Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query AD for home directory

Status
Not open for further replies.

jlotz1

Technical User
Feb 1, 2010
6
US
Hello everyone,
I have a list of 167 user ID's. I need to query active directory and return a list that shows the user ID and their home directory path. For example:

jbsmith \\server1\home\jbsmith
jxjones \\server1\home\jxjones

Outputting a text file would work, but .csv or even excel would be helpful.

This is what I have so far:

On Error Resume Next
Set objUser = GetObject _
("LDAP://cn=testuser,ou=_USERS,dc=domain,dc=com")
objUser.GetInfo

strHomeDirectory = objUser.Get("homeDirectory")
strUserId = objUser.sAMAccountName
WScript.Echo "userid: " & strUserId
WScript.echo "homeDirectory: " & strHomeDirectory

How do I modify that so I can query from a txt file containing a list of user ID's? As it is now I'd have to run this 167 times. And how can I make it so that it outputs to .csv or .xls?

Thanks in advance for any advice!!
 
'place your working code into a sub, then call that many times based on the list of your users you have read from a txt file
'if you start the script with something like
'cscript.exe myscript.vbs > c:\results.csv
'it will write the csv for you?


strFile = "c:\users.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objTS = FSO.OpenTextFile(strFile, 1, False) 'can never remember
Do While Not objTS.AtEndOfStream
strLine = ""
strPassed = ""
strLine = objTS.ReadLine
'Wscript.Echo strLine
If strLine <> "" Then
Call GetUserInfo(strLine, strPassed)
Wscript.Echo strPassed
End If
Loop
objTS.Close
Set objTS = Nothing
Set FSO = Nothing

Sub GetUserInfo(ByVal strUser, ByRef str2GiveBack)
On Error Resume Next
Set objUser = GetObject _
("LDAP://cn=" & strUser & ",ou=_USERS,dc=domain,dc=com")
objUser.GetInfo
strHomeDirectory = objUser.Get("homeDirectory")
strUserId = objUser.sAMAccountName
str2GiveBack = strUserId & "," & strHomeDirectory
'WScript.Echo "userid: " & strUserId
'WScript.echo "homeDirectory: " & strHomeDirectory
End Sub

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
mrmovie you are awesome! The only problem I just noticed is that my c:\users.txt file must contain the CN of the user (Robert. P. Smith) for it to work. I'd rather be able to work from a list of user ID's (rpsmith). Can your script be tweaked to allow for that?
 
i am guessing this line could be changed

Set objUser = GetObject _
("LDAP://cn=" & strUser & ",ou=_USERS,dc=domain,dc=com")

to something like

Set objUser = GetObject("WinNT://domainname/" & strUser & ",user")



I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top