Solved

Reading a text file then run DSQuery then output to text file

Posted on 2011-03-24
13
1,818 Views
Last Modified: 2012-05-11
Hi Team,
I need a assist with coding for VBS + Dsquery, Here what i need it to do.
1. Read a text file for input line by line (possible to have a string).
2. Then the input then would run against the following command (to capture any Distingushname result)
dsquery ou forestroot -name -o dn
dsquery user forestroot -name -o dn
dsquery group forestroot -name -o dn
3. If there is a result, capture the distingushname then output it to another text file.

I have the following starting code for reading the input but not further on how to progress below.
Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
Set objOutputFile = objFSO.OpenTextFile("C:\scripts\output.txt",8,True)

Do until objInputFile.AtEndofStream
	strcomputer = objInputFile.ReadLine
	strCommand = "dsquery ou forestroot -name " & strComputer "-o dn"
	Set oExec = WShShell.Exec(strCommand)
	If (oExec.Status = 0) Then
		If (oExec.stdOut.ReadAll = "") Then
			objOutputFile.WriteLine(strComputer)
		End If
	End If
Loop

objInputFile.Close
objOutputFile.Close

Open in new window

Base Code from Ravikanth.Chaganti
0
Comment
Question by:AhMike
  • 8
  • 5
13 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35212550
Hi, I haven't tested it, but does this work?

Regards,

Rob.
Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\scripts\computers.txt",1)
strOutput = "C:\scripts\output.txt"

Do until objInputFile.AtEndofStream
	strLine= objInputFile.ReadLine
	strCommand = "cmd /c dsquery ou forestroot -name " & strLine & " -o dn | find ""OU="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery user forestroot -name " & strLine & " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery group forestroot -name " & strLine & " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
Loop

objInputFile.Close
objOutputFile.Close

MsgBox "Done"

Open in new window

0
 

Author Comment

by:AhMike
ID: 35212614
testing it now... 1 moment
0
 

Author Comment

by:AhMike
ID: 35212628
Getting a runtime error :

Line 19
Error: Object required : 'objOutputfile'
Code :800A01A8
0
 

Author Comment

by:AhMike
ID: 35212662
Hi Rob,

Thanks abit tweak and you code work... but strange

group and user seems to be working fine. However there no return value for OU. Anyway i will award you the points 1st.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35212772
Hi, thanks for the grade.  That is a bit odd about the OUs....if you run the command from a command prompt manually, does it find it?

Rob.
0
 

Author Comment

by:AhMike
ID: 35213019
when i run a manually with this : it will return the results.
dsquery ou forestroot -name DRA* -o dn
"OU=meow Groups,OU=Group Directory,DC=abc-ap,DC=office,DC=home,DC=com"

Could it because the find was skip bcos of two OU ?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:AhMike
ID: 35213064
Hmmm if i insert

List File :
abc  
BCD Group


The BCD Group Ou that cant be searched
0
 

Author Comment

by:AhMike
ID: 35213141
I'm adding a & chr(34) to create " string " ..  but still getting a syntax error :

Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\temp\mike\list.txt",1)
strOutput = "C:\temp\mike\output.txt"

Do until objInputFile.AtEndofStream
	strLine= objInputFile.ReadLine
	strCommand = "cmd /c dsquery ou forestroot -name " & Chr(34) & strLine & Chr(34) " -o dn | find ""OU="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery user forestroot -name " & chr(34) & strLine & chr(34) " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery group forestroot -name " & chr(34) & strLine & chr(34) " -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
Loop

objInputFile.Close


MsgBox "Done"

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35213815
You won't need the Chr(34).

If you are running DRA* in your manual search, then try putting
DRA*

in your text file.

Regards,

Rob.
0
 

Author Comment

by:AhMike
ID: 35228495
Hi Rob,

If i need string ... how should i run it ? lets say the group name is Class A instead of ClassA
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35228557
Oh, I understand....sorry, missed that....try this.

Rob.
Do until objInputFile.AtEndofStream
	strLine= objInputFile.ReadLine
	strCommand = "cmd /c dsquery ou forestroot -name """ & strLine & """ -o dn | find ""OU="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery user forestroot -name """ & strLine & """ -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
	strCommand = "cmd /c dsquery group forestroot -name """ & strLine & """ -o dn | find ""CN="" >> " & strOutput
	WShShell.Run strCommand, 0, True
Loop

Open in new window

0
 

Author Comment

by:AhMike
ID: 35311677
IT WORKED!!! THANKS ROB !
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35311709
No problem. Glad to help.

Rob.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Installing a printer using group policy preferences is not that hard let’s take a look at it. First lets open up your group policy console and edit the policy you want to add it to. I recommend creating a new policy for each printer makes it a l…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now