Link to home
Start Free TrialLog in
Avatar of SnAkEhIpS
SnAkEhIpSFlag for United States of America

asked on

Find all users in domain with home directory on the same server -VBScript-

Trying to find all users in Active Directory who have their home directory mapped to a particular server . For example:

I want to know all users who have their home directory mapped to ServerXYZ. All user home directory paths in the domain take the exact same form: \\Server\Users\UserName\Home
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, try this.  You can set strOU to a blank string to enumerate the entire domain.

Regards,

Rob.
strOU = "OU=Sites,"
strHomeDirectoryBase = "\\YourFileServer\Users\"
strOutputFile = "HomeFolderUsers.txt"

If Trim(strOU) <> "" Then
	If Right(strOU, 1) <> "," Then strOU = strOU & ","
End If
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

strFilter = "(&(objectCategory=person)(objectClass=user)(homeDirectory=" & Replace(strHomeDirectoryBase, "\", "\5c") & "*))"
strAttributes = "samAccountName,cn,adsPath,homeDirectory"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strOutputFile, True)

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
	objOutput.WriteLine adoRecordset.Fields("samAccountName") & "," & adoRecordset.Fields("homeDirectory")
	adoRecordset.MoveNext
Loop
adoRecordset.Close

objOutput.Close

MsgBox "Done. Please see " & strOutputFile

Open in new window

Avatar of SnAkEhIpS

ASKER

I tried the dn of the OU and "", but nothing was written to the outputfile. It was created, but empty - 0KB.
Hmmm, it works for me.  If you set strOU = "" it will scan the entire domain for any user home directory that *starts with* whatever you put for strHomeDirectoryBase

You could try it as just the server name to see if it outputs better.

strHomeDirectoryBase = "\\YOURFILESERVER\"

Regards,

Rob.
Still nothing. I can run other scripts against AD. If I couldn't connect, then I should see some error message shouldn't I?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
strBase = "<LDAP://" & strOU & strDNSDomain & ">"

That did it :-)  Thank you very much.
Good to hear. Thanks for the grade.

Rob.