VBscript code to search AD users from a source txt file and write to output txt file

bndit
bndit used Ask the Experts™
on
Hello,

I need some help with creating a VBscript that can help me search for AD users. I'm having a hard time piecing this script together mostly because I'm new to scripting so bare with me. My goal is to build this script to check whether or not the user exists in AD based on the sAMAccountName AD attribute (although it could be some other attribute). The output will tell me whether the user is or is not in AD, simple right? I searched the internet and got people's code out there and pieced something together that is able to read from a txt file and tell me whether or not the user exists. However, the output is to the screen and not to a file. Any help will be much appreciated.
Thanks,
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\textfile.txt", ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    
strUserName = arrServiceList(0)
dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
objCommand.CommandText = _
    "<LDAP://dc=wiredbrain,dc=local>;(&(objectCategory=User)" & _
         "(samAccountName=" & strUserName & "));samAccountName;subtree"
  
Set objRecordSet = objCommand.Execute
 
If objRecordset.RecordCount = 0 Then
    WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
Else
    WScript.Echo strUserName & " exists."
End If
 
objConnection.Close
 
Loop

Open in new window

new-file
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
change your wscript.echo statements to objTextFile.Write
And also add this

Set objTextFile = objFSO.CreateTextFile("C:\")

Set objTextFile = objFSO.CreateTextFile("C:\sometextfile.txt")
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thanks for the quick response. I did what you suggested, but didn't work. I'm attaching the modified code; I'm sure I put something in the wrong place or missed something.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\textfile.txt", ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    'For i = 1 to Ubound(arrServiceList)
    '    Wscript.Echo "Service: " & arrServiceList(i)
    'Next
'Loop
 
strUserName = arrServiceList(0)
dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
objCommand.CommandText = _
    "<LDAP://dc=wiredbrain,dc=local>;(&(objectCategory=User)" & _
         "(samAccountName=" & strUserName & "));samAccountName;subtree"
  
Set objRecordSet = objCommand.Execute
Set objTextFile = objFSO.CreateTextFile("c:\output.txt")
 
If objRecordset.RecordCount = 0 Then
    objTextFile.Write strUserName & " does not exist."
Else
    objTextFile.Write strUserName & " exists."
End If
 
Loop 
objConnection.Close

Open in new window

LAX-DC1---2009-07-21-17-03-31.png
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hi, I have followed Solomon's approach and this should work for you.  The text file is created at the top, and we use objOutputFile.WriteLine instead of WScript.Echo to write to the file.

Regards,

Rob.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\textfile.txt", ForReading)
Set objOutputFile = objFSO.CreateTextFile("c:\user_results.txt", True)
Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	arrServiceList = Split(strNextLine , ",")
	strUserName = arrServiceList(0)
	dtStart = TimeValue(Now())
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Open "Provider=ADsDSOObject;"
 
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
 
	objCommand.CommandText = _
	    "<LDAP://dc=wiredbrain,dc=local>;(&(objectCategory=User)" & _
		"(samAccountName=" & strUserName & "));samAccountName;subtree"
  
	Set objRecordSet = objCommand.Execute
	If objRecordset.RecordCount = 0 Then
	    'WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
	    objOutputFile.WriteLine "sAMAccountName: " & strUserName & " does not exist."
	Else
	    objOutputFile.WriteLine strUserName & " exists."
	End If
	objRecordSet.Close
	
	objConnection.Close 
Loop
 
objOutputFile.Close
objTextFile.Close
 
MsgBox "Finished. Please check output file."

Open in new window

Author

Commented:
Thanks

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