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

SnAkEhIpS
SnAkEhIpS used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
I tried the dn of the OU and "", but nothing was written to the outputfile. It was created, but empty - 0KB.
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
Still nothing. I can run other scripts against AD. If I couldn't connect, then I should see some error message shouldn't I?
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hmmm..what are you putting for strOU? Just the DN, without the DC parts?  That's what you need.

So, for example, only
strOU = "OU=Sites,"

instead of
strOU = "OU=Sites,DC=Domain,DC=Com"

But also, I forgot to actually use that.  Change this line:
strBase = "<LDAP://" & strDNSDomain & ">"

to this:
strBase = "<LDAP://" & strOU & strDNSDomain & ">"

to point to that OU only.

Either way though, it still sounds like it's not matching your HomeDirectory value.  We are talking Home Directory, and not Profile Paths right?

Maybe just try using
strHomeDirectoryBase = "\\YourFileServer\Users\"

to see if it pulls all accounts that have anything specified.

Regards,

Rob.

Author

Commented:
strBase = "<LDAP://" & strOU & strDNSDomain & ">"

That did it :-)  Thank you very much.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Good to hear. Thanks for the grade.

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial