Solved

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

Posted on 2011-03-24
13
1,895 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Suggested Solutions

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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