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
VB Script

Avatar of undefined
Last Comment
RobSampson
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
SnAkEhIpS
Flag of United States of America image

ASKER

I tried the dn of the OU and "", but nothing was written to the outputfile. It was created, but empty - 0KB.
Avatar of RobSampson
RobSampson
Flag of Australia image

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.
Avatar of SnAkEhIpS
SnAkEhIpS
Flag of United States of America image

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SnAkEhIpS
SnAkEhIpS
Flag of United States of America image

ASKER

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

That did it :-)  Thank you very much.
Avatar of RobSampson
RobSampson
Flag of Australia image

Good to hear. Thanks for the grade.

Rob.
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo