Solved

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

Posted on 2011-03-24
13
1,861 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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 process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

770 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