Solved

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

Posted on 2011-03-24
13
1,878 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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